Add support for enabling encryption by default
This commit is contained in:
@@ -20,7 +20,8 @@ from mautrix.types import (Event, EventType, RoomID, UserID, EventID, ReceiptEve
|
||||
ReceiptEventContent, PresenceEvent, PresenceState, TypingEvent,
|
||||
MessageEvent, StateEvent, RedactionEvent, RoomNameStateEventContent,
|
||||
RoomAvatarStateEventContent, RoomTopicStateEventContent,
|
||||
MemberStateEventContent, EncryptedEvent)
|
||||
MemberStateEventContent, EncryptedEvent, TextMessageEventContent,
|
||||
MessageType)
|
||||
from mautrix.errors import MatrixError
|
||||
|
||||
from . import user as u, portal as po, puppet as pu, commands as com
|
||||
@@ -38,7 +39,7 @@ except ImportError:
|
||||
EVENT_TIME = None
|
||||
|
||||
try:
|
||||
from .e2ee import EncryptionManager
|
||||
from mautrix.bridge.e2ee import EncryptionManager
|
||||
except ImportError:
|
||||
EncryptionManager = None
|
||||
|
||||
@@ -57,10 +58,15 @@ class MatrixHandler(BaseMatrixHandler):
|
||||
command_processor=com.CommandProcessor(context))
|
||||
self.e2ee = None
|
||||
if self.config["bridge.encryption.allow"]:
|
||||
if EncryptionManager:
|
||||
self.e2ee = EncryptionManager(context)
|
||||
if not EncryptionManager:
|
||||
self.log.error("Encryption enabled in config, but dependencies not installed.")
|
||||
elif not self.config["bridge.login_shared_secret"]:
|
||||
self.log.warning("Encryption enabled in config, but login_shared_secret not set.")
|
||||
else:
|
||||
self.log.warning("Encryption enabled in config, but dependencies not installed.")
|
||||
self.e2ee = EncryptionManager(
|
||||
bot_mxid=self.az.bot_mxid,
|
||||
login_shared_secret=self.config["bridge.login_shared_secret"],
|
||||
homeserver_address=self.config["homeserver.address"], loop=context.loop)
|
||||
self.bot = context.bot
|
||||
self.previously_typing = {}
|
||||
|
||||
@@ -121,14 +127,50 @@ class MatrixHandler(BaseMatrixHandler):
|
||||
except MatrixError:
|
||||
pass
|
||||
portal.mxid = room_id
|
||||
e2be_ok = None
|
||||
if self.config["bridge.encryption.default"] and self.e2ee:
|
||||
e2be_ok = await self._enable_dm_encryption(portal)
|
||||
portal.save()
|
||||
inviter.register_portal(portal)
|
||||
await intent.send_notice(room_id, "Portal to private chat created.")
|
||||
if e2be_ok is True:
|
||||
evt_type, content = await self.e2ee.encrypt(
|
||||
room_id, EventType.ROOM_MESSAGE,
|
||||
TextMessageEventContent(msgtype=MessageType.NOTICE,
|
||||
body="Portal to private chat created and end-to-bridge"
|
||||
" encryption enabled."))
|
||||
await intent.send_message_event(room_id, evt_type, content)
|
||||
else:
|
||||
message = "Portal to private chat created."
|
||||
if e2be_ok is False:
|
||||
message += "\n\nWarning: Failed to enable end-to-bridge encryption"
|
||||
await intent.send_notice(room_id, message)
|
||||
else:
|
||||
await intent.join_room(room_id)
|
||||
await intent.send_notice(room_id, "This puppet will remain inactive until a "
|
||||
"Telegram chat is created for this room.")
|
||||
|
||||
async def _enable_dm_encryption(self, portal: po.Portal) -> bool:
|
||||
try:
|
||||
await portal.main_intent.invite_user(portal.mxid, self.az.bot_mxid)
|
||||
await self.az.intent.join_room_by_id(portal.mxid)
|
||||
await portal.main_intent.send_state_event(portal.mxid, EventType.ROOM_ENCRYPTION, {
|
||||
"algorithm": "m.megolm.v1.aes-sha2"
|
||||
})
|
||||
# TODO feed info about room to matrix-nio
|
||||
except Exception:
|
||||
self.log.warning(f"Failed to enable end-to-bridge encryption in {portal.mxid}",
|
||||
exc_info=True)
|
||||
return False
|
||||
|
||||
try:
|
||||
puppet = pu.Puppet.get(portal.tgid)
|
||||
await portal.main_intent.set_room_name(portal.mxid, puppet.displayname)
|
||||
except Exception:
|
||||
self.log.warning(f"Failed to set room name for {portal.mxid}", exc_info=True)
|
||||
|
||||
portal.encrypted = True
|
||||
return True
|
||||
|
||||
async def send_welcome_message(self, room_id: RoomID, inviter: 'u.User') -> None:
|
||||
try:
|
||||
is_management = len(await self.az.intent.get_room_members(room_id)) == 2
|
||||
@@ -173,7 +215,7 @@ class MatrixHandler(BaseMatrixHandler):
|
||||
"messages for unauthenticated users.")
|
||||
return
|
||||
|
||||
self.log.debug(f"{user} joined {room_id}")
|
||||
self.log.debug(f"{user.mxid} joined {room_id}")
|
||||
if await user.is_logged_in() or portal.has_bot:
|
||||
await portal.join_matrix(user, event_id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user