Browse Source

Enable double puppeting with shared secret login

Tulir Asokan 4 years ago
parent
commit
c92356cf8b
5 changed files with 25 additions and 4 deletions
  1. 1 1
      ROADMAP.md
  2. 3 1
      mautrix_signal/portal.py
  3. 3 0
      mautrix_signal/puppet.py
  4. 17 1
      mautrix_signal/user.py
  5. 1 1
      requirements.txt

+ 1 - 1
ROADMAP.md

@@ -49,7 +49,7 @@
   * [ ] Provisioning API for logging in
   * [ ] Provisioning API for logging in
   * [ ] Private chat creation by inviting Matrix puppet of Signal user to new room
   * [ ] Private chat creation by inviting Matrix puppet of Signal user to new room
   * [ ] Option to use own Matrix account for messages sent from other Signal clients
   * [ ] Option to use own Matrix account for messages sent from other Signal clients
-    * [ ] Automatic login with shared secret
+    * [x] Automatic login with shared secret
     * [ ] Manual login with `login-matrix`
     * [ ] Manual login with `login-matrix`
   * [x] E2EE in Matrix rooms
   * [x] E2EE in Matrix rooms
 
 

+ 3 - 1
mautrix_signal/portal.py

@@ -577,7 +577,9 @@ class Portal(DBPortal, BasePortal):
             })
             })
             if self.is_direct:
             if self.is_direct:
                 invites.append(self.az.bot_mxid)
                 invites.append(self.az.bot_mxid)
-        if self.encrypted or self.private_chat_portal_meta or not self.is_direct:
+        if source.uuid == self.chat_id:
+            name = self.name = "Signal Note to Self"
+        elif self.encrypted or self.private_chat_portal_meta or not self.is_direct:
             name = self.name
             name = self.name
         if self.config["appservice.community_id"]:
         if self.config["appservice.community_id"]:
             initial_state.append({
             initial_state.append({

+ 3 - 0
mautrix_signal/puppet.py

@@ -181,6 +181,9 @@ class Puppet(DBPuppet, BasePuppet):
 
 
     async def _update_portal_names(self) -> None:
     async def _update_portal_names(self) -> None:
         async for portal in p.Portal.find_private_chats_with(self.uuid):
         async for portal in p.Portal.find_private_chats_with(self.uuid):
+            if portal.receiver == self.number:
+                # This is a note to self chat, don't change the name
+                continue
             await portal.update_puppet_name(self.name)
             await portal.update_puppet_name(self.name)
 
 
     async def default_puppet_should_leave_room(self, room_id: RoomID) -> bool:
     async def default_puppet_should_leave_room(self, room_id: RoomID) -> bool:

+ 17 - 1
mautrix_signal/user.py

@@ -17,7 +17,7 @@ from typing import Dict, Optional, AsyncGenerator, TYPE_CHECKING, cast
 from uuid import UUID
 from uuid import UUID
 import asyncio
 import asyncio
 
 
-from mausignald.types import Account
+from mausignald.types import Account, Address
 from mautrix.bridge import BaseUser
 from mautrix.bridge import BaseUser
 from mautrix.types import UserID, RoomID
 from mautrix.types import UserID, RoomID
 from mautrix.appservice import AppService
 from mautrix.appservice import AppService
@@ -60,6 +60,12 @@ class User(DBUser, BaseUser):
         cls.az = bridge.az
         cls.az = bridge.az
         cls.loop = bridge.loop
         cls.loop = bridge.loop
 
 
+    @property
+    def address(self) -> Optional[Address]:
+        if not self.username:
+            return None
+        return Address(uuid=self.uuid, number=self.username)
+
     async def on_signin(self, account: Account) -> None:
     async def on_signin(self, account: Account) -> None:
         self.username = account.username
         self.username = account.username
         self.uuid = account.uuid
         self.uuid = account.uuid
@@ -67,7 +73,17 @@ class User(DBUser, BaseUser):
         await self.bridge.signal.subscribe(self.username)
         await self.bridge.signal.subscribe(self.username)
         self.loop.create_task(self.sync())
         self.loop.create_task(self.sync())
 
 
+    async def _sync_puppet(self) -> None:
+        puppet = await pu.Puppet.get_by_address(self.address)
+        if puppet.custom_mxid != self.mxid and puppet.can_auto_login(self.mxid):
+            self.log.info(f"Automatically enabling custom puppet")
+            await puppet.switch_mxid(access_token="auto", mxid=self.mxid)
+
     async def sync(self) -> None:
     async def sync(self) -> None:
+        try:
+            await self._sync_puppet()
+        except Exception:
+            self.log.exception("Error while syncing own puppet")
         try:
         try:
             await self._sync()
             await self._sync()
         except Exception:
         except Exception:

+ 1 - 1
requirements.txt

@@ -4,5 +4,5 @@ commonmark>=0.8,<0.10
 aiohttp>=3,<4
 aiohttp>=3,<4
 yarl>=1,<2
 yarl>=1,<2
 attrs>=19.1
 attrs>=19.1
-mautrix>=0.7.9,<0.8
+mautrix>=0.7.10,<0.8
 asyncpg>=0.20,<0.22
 asyncpg>=0.20,<0.22