matrix.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 List, Union, TYPE_CHECKING
  17. from mautrix.bridge import BaseMatrixHandler
  18. from mautrix.types import (Event, ReactionEvent, MessageEvent, StateEvent, EncryptedEvent, RoomID,
  19. EventID, UserID, ReactionEventContent, RelationType, EventType,
  20. ReceiptEvent, TypingEvent, PresenceEvent, RedactionEvent,
  21. SingleReceiptEventContent)
  22. from .db import Message as DBMessage
  23. from . import puppet as pu, portal as po, user as u, signal as s
  24. if TYPE_CHECKING:
  25. from .__main__ import SignalBridge
  26. class MatrixHandler(BaseMatrixHandler):
  27. signal: 's.SignalHandler'
  28. def __init__(self, bridge: 'SignalBridge') -> None:
  29. prefix, suffix = bridge.config["bridge.username_template"].format(userid=":").split(":")
  30. homeserver = bridge.config["homeserver.domain"]
  31. self.user_id_prefix = f"@{prefix}"
  32. self.user_id_suffix = f"{suffix}:{homeserver}"
  33. self.signal = bridge.signal
  34. super().__init__(bridge=bridge)
  35. def filter_matrix_event(self, evt: Event) -> bool:
  36. if not isinstance(evt, (ReactionEvent, MessageEvent, StateEvent, EncryptedEvent,
  37. RedactionEvent, ReceiptEvent, TypingEvent)):
  38. return True
  39. return (evt.sender == self.az.bot_mxid
  40. or pu.Puppet.get_id_from_mxid(evt.sender) is not None)
  41. async def send_welcome_message(self, room_id: RoomID, inviter: 'u.User') -> None:
  42. await super().send_welcome_message(room_id, inviter)
  43. if not inviter.notice_room:
  44. inviter.notice_room = room_id
  45. await inviter.update()
  46. await self.az.intent.send_notice(room_id, "This room has been marked as your "
  47. "Signal bridge notice room.")
  48. async def handle_leave(self, room_id: RoomID, user_id: UserID, event_id: EventID) -> None:
  49. portal = await po.Portal.get_by_mxid(room_id)
  50. if not portal:
  51. return
  52. user = await u.User.get_by_mxid(user_id, create=False)
  53. if not user:
  54. return
  55. await portal.handle_matrix_leave(user)
  56. @classmethod
  57. async def handle_reaction(cls, room_id: RoomID, user_id: UserID, event_id: EventID,
  58. content: ReactionEventContent) -> None:
  59. if content.relates_to.rel_type != RelationType.ANNOTATION:
  60. cls.log.debug(f"Ignoring m.reaction event in {room_id} from {user_id} with unexpected "
  61. f"relation type {content.relates_to.rel_type}")
  62. return
  63. user = await u.User.get_by_mxid(user_id)
  64. if not user:
  65. return
  66. portal = await po.Portal.get_by_mxid(room_id)
  67. if not portal:
  68. return
  69. await portal.handle_matrix_reaction(user, event_id, content.relates_to.event_id,
  70. content.relates_to.key)
  71. async def handle_read_receipt(self, user: 'u.User', portal: 'po.Portal', event_id: EventID,
  72. data: SingleReceiptEventContent) -> None:
  73. message = await DBMessage.get_by_mxid(event_id, portal.mxid)
  74. if not message:
  75. return
  76. user.log.trace(f"Sending read receipt for {message.timestamp} to {message.sender}")
  77. await self.signal.send_receipt(user.username, message.sender,
  78. timestamps=[message.timestamp], when=data.ts, read=True)
  79. async def handle_typing(self, room_id: RoomID, typing: List[UserID]) -> None:
  80. pass
  81. # portal = await po.Portal.get_by_mxid(room_id)
  82. # if not portal:
  83. # return
  84. #
  85. # for user_id in typing:
  86. # user = await u.User.get_by_mxid(user_id, create=False)
  87. # if not user or not user.username:
  88. # continue
  89. # # TODO
  90. async def handle_event(self, evt: Event) -> None:
  91. if evt.type == EventType.REACTION:
  92. evt: ReactionEvent
  93. await self.handle_reaction(evt.room_id, evt.sender, evt.event_id, evt.content)
  94. async def handle_ephemeral_event(self, evt: Union[ReceiptEvent, PresenceEvent, TypingEvent]
  95. ) -> None:
  96. if evt.type == EventType.TYPING:
  97. await self.handle_typing(evt.room_id, evt.content.user_ids)
  98. else:
  99. await super().handle_ephemeral_event(evt)