signal.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 json
  19. from mautrix.bridge.commands import HelpSection, command_handler, SECTION_ADMIN
  20. from mausignald.types import Address
  21. from .. import puppet as pu, portal as po
  22. from .auth import make_qr
  23. from .typehint import CommandEvent
  24. try:
  25. import qrcode
  26. import PIL as _
  27. except ImportError:
  28. qrcode = None
  29. SECTION_SIGNAL = HelpSection("Signal actions", 20, "")
  30. remove_extra_chars = str.maketrans("", "", " .,-()")
  31. async def _get_puppet_from_cmd(evt: CommandEvent) -> Optional['pu.Puppet']:
  32. if len(evt.args) == 0 or not evt.args[0].startswith("+"):
  33. await evt.reply("**Usage:** `$cmdprefix+sp pm <phone>` "
  34. "(enter phone number in international format)")
  35. return None
  36. phone = "".join(evt.args).translate(remove_extra_chars)
  37. if not phone[1:].isdecimal():
  38. await evt.reply("**Usage:** `$cmdprefix+sp pm <phone>` "
  39. "(enter phone number in international format)")
  40. return None
  41. return await pu.Puppet.get_by_address(Address(number=phone))
  42. def _format_safety_number(number: str) -> str:
  43. line_size = 20
  44. chunk_size = 5
  45. return "\n".join(" ".join([number[chunk:chunk + chunk_size]
  46. for chunk in range(line, line + line_size, chunk_size)])
  47. for line in range(0, len(number), line_size))
  48. def _pill(puppet: 'pu.Puppet') -> str:
  49. return f"[{puppet.name}](https://matrix.to/#/{puppet.mxid})"
  50. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_SIGNAL,
  51. help_text="Open a private chat portal with a specific phone number",
  52. help_args="<_phone_>")
  53. async def pm(evt: CommandEvent) -> None:
  54. puppet = await _get_puppet_from_cmd(evt)
  55. if not puppet:
  56. return
  57. portal = await po.Portal.get_by_chat_id(puppet.address, receiver=evt.sender.username,
  58. create=True)
  59. if portal.mxid:
  60. await evt.reply(f"You already have a private chat with {puppet.name}: "
  61. f"[{portal.mxid}](https://matrix.to/#/{portal.mxid})")
  62. await portal.main_intent.invite_user(portal.mxid, evt.sender.mxid)
  63. return
  64. await portal.create_matrix_room(evt.sender, puppet.address)
  65. await evt.reply(f"Created a portal room with {_pill(puppet)} and invited you to it")
  66. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_SIGNAL,
  67. help_text="View the safety number of a specific user",
  68. help_args="[--qr] [_phone_]")
  69. async def safety_number(evt: CommandEvent) -> None:
  70. show_qr = evt.args and evt.args[0].lower() == "--qr"
  71. if show_qr:
  72. if not qrcode:
  73. await evt.reply("Can't generate QR code: qrcode and/or PIL not installed")
  74. return
  75. evt.args = evt.args[1:]
  76. if len(evt.args) == 0 and evt.portal and evt.portal.is_direct:
  77. puppet = await pu.Puppet.get_by_address(evt.portal.chat_id)
  78. else:
  79. puppet = await _get_puppet_from_cmd(evt)
  80. if not puppet:
  81. return
  82. resp = await evt.bridge.signal.get_identities(evt.sender.username, puppet.address)
  83. if not resp.identities:
  84. await evt.reply(f"No identities found for {_pill(puppet)}")
  85. return
  86. most_recent = resp.identities[0]
  87. for identity in resp.identities:
  88. if identity.added > most_recent.added:
  89. most_recent = identity
  90. uuid = most_recent.address.uuid or "unknown"
  91. await evt.reply(f"### {puppet.name}\n\n"
  92. f"**UUID:** {uuid} \n"
  93. f"**Trust level:** {most_recent.trust_level} \n"
  94. f"**Safety number:**\n"
  95. f"```\n{_format_safety_number(most_recent.safety_number)}\n```")
  96. if show_qr and most_recent.qr_code_data:
  97. data = base64.b64decode(most_recent.qr_code_data)
  98. content = await make_qr(evt.main_intent, data, "verification-qr.png")
  99. await evt.main_intent.send_message(evt.room_id, content)
  100. @command_handler(needs_admin=True, needs_auth=False, help_section=SECTION_ADMIN,
  101. help_text="Send raw requests to signald", help_args="[--user] <type> <_json_>")
  102. async def raw(evt: CommandEvent) -> None:
  103. add_username = False
  104. if evt.args[0].lower() == "--user":
  105. add_username = True
  106. evt.args = evt.args[1:]
  107. type = evt.args[0]
  108. try:
  109. args = json.loads(" ".join(evt.args[1:]))
  110. except json.JSONDecodeError as e:
  111. await evt.reply(f"JSON decode error: {e}")
  112. return
  113. if add_username:
  114. args["username"] = evt.sender.username
  115. try:
  116. resp_type, resp_data = await evt.bridge.signal._raw_request(type, **args)
  117. except Exception as e:
  118. await evt.reply(f"Error sending request: {e}")
  119. else:
  120. if resp_data is None:
  121. await evt.reply(f"Got reply `{resp_type}` with no content")
  122. else:
  123. await evt.reply(f"Got reply `{resp_type}`:\n\n"
  124. f"```json\n{json.dumps(resp_data, indent=2)}\n```")