Tulir Asokan 4 жил өмнө
parent
commit
ccf7773dba

+ 2 - 2
mauigpapi/errors/response.py

@@ -70,11 +70,11 @@ class IGNotLoggedInError(IGResponseError):
 
 
 
 
 class IGUserHasLoggedOutError(IGNotLoggedInError):
 class IGUserHasLoggedOutError(IGNotLoggedInError):
-    body: LoginRequiredResponse
+    pass
 
 
 
 
 class IGLoginRequiredError(IGNotLoggedInError):
 class IGLoginRequiredError(IGNotLoggedInError):
-    body: LoginRequiredResponse
+    pass
 
 
 
 
 class IGPrivateUserError(IGResponseError):
 class IGPrivateUserError(IGResponseError):

+ 1 - 0
mautrix_instagram/__main__.py

@@ -25,6 +25,7 @@ from .portal import Portal
 from .puppet import Puppet
 from .puppet import Puppet
 from .matrix import MatrixHandler
 from .matrix import MatrixHandler
 from .version import version, linkified_version
 from .version import version, linkified_version
+from . import commands
 
 
 
 
 class InstagramBridge(Bridge):
 class InstagramBridge(Bridge):

+ 2 - 0
mautrix_instagram/commands/__init__.py

@@ -0,0 +1,2 @@
+from .auth import SECTION_AUTH
+from .conn import SECTION_CONNECTION

+ 105 - 0
mautrix_instagram/commands/auth.py

@@ -0,0 +1,105 @@
+# mautrix-twitter - A Matrix-Twitter DM puppeting bridge
+# Copyright (C) 2020 Tulir Asokan
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+from mauigpapi.state import AndroidState
+from mauigpapi.http import AndroidAPI
+from mauigpapi.errors import (IGLoginTwoFactorRequiredError, IGLoginBadPasswordError,
+                              IGLoginInvalidUserError, IGLoginError)
+
+from mautrix.bridge.commands import HelpSection, command_handler
+from .typehint import CommandEvent
+
+SECTION_AUTH = HelpSection("Authentication", 10, "")
+
+
+@command_handler(needs_auth=False, management_only=True, help_section=SECTION_AUTH,
+                 help_text="Log in to Instagram", help_args="<_username_> <_password_>")
+async def login(evt: CommandEvent) -> None:
+    if await evt.sender.is_logged_in():
+        await evt.reply("You're already logged in")
+        return
+    elif len(evt.args) < 2:
+        await evt.reply("**Usage:** `$cmdprefix+sp login <username> <password>`")
+        return
+    username = evt.args[0]
+    password = " ".join(evt.args[1:])
+    state = AndroidState()
+    state.device.generate(username)
+    api = AndroidAPI(state)
+    await api.simulate_pre_login_flow()
+    try:
+        resp = await api.login(username, password)
+    except IGLoginTwoFactorRequiredError as e:
+        tfa_info = e.body.two_factor_info
+        msg = "Username and password accepted, but you have two-factor authentication enabled.\n"
+        if tfa_info.sms_two_factor_on:
+            if tfa_info.totp_two_factor_on:
+                msg += (f"Send the code from your authenticator app "
+                        f"or one sent to {tfa_info.obfuscated_phone_number} here.")
+            else:
+                msg += f"Send the code sent to {tfa_info.obfuscated_phone_number} here."
+        elif tfa_info.totp_two_factor_on:
+            msg += "Send the code from your authenticator app here."
+        else:
+            msg += ("Unfortunately, none of your two-factor authentication methods are currently "
+                    "supported by the bridge.")
+            return
+        evt.sender.command_status = {
+            "action": "Login",
+            "room_id": evt.room_id,
+            "next": enter_login_2fa,
+
+            "username": tfa_info.username,
+            "2fa_identifier": tfa_info.two_factor_identifier,
+            "state": state,
+            "api": api,
+        }
+    except IGLoginInvalidUserError:
+        await evt.reply("Invalid username")
+    except IGLoginBadPasswordError:
+        await evt.reply("Incorrect password")
+    else:
+        evt.sender.state = state
+        user = resp.logged_in_user
+        await evt.reply(f"Successfully logged in as {user.full_name} ([@{user.username}]"
+                        f"(https://instagram.com/{user.username}), user ID: {user.pk})")
+        await evt.sender.try_connect()
+
+
+async def enter_login_2fa(evt: CommandEvent) -> None:
+    api: AndroidAPI = evt.sender.command_status["api"]
+    state: AndroidState = evt.sender.command_status["state"]
+    identifier = evt.sender.command_status["2fa_identifier"]
+    username = evt.sender.command_status["username"]
+    evt.sender.command_status = None
+    try:
+        user = await api.two_factor_login(username, code="".join(evt.args), identifier=identifier)
+    except IGLoginError as e:
+        await evt.reply(f"Failed to log in: {e.body.message}")
+    except Exception as e:
+        await evt.reply(f"Failed to log in: {e}")
+        evt.log.exception("Failed to log in")
+    else:
+        evt.sender.state = state
+        await evt.reply(f"Successfully logged in as {user.full_name} ([@{user.username}]"
+                        f"(https://instagram.com/{user.username}), user ID: {user.pk})")
+        await evt.sender.try_connect()
+
+
+@command_handler(needs_auth=True, help_section=SECTION_AUTH, help_text="Disconnect the bridge from"
+                                                                       "your Instagram account")
+async def logout(evt: CommandEvent) -> None:
+    await evt.sender.logout()
+    await evt.reply("Successfully logged out")

+ 55 - 0
mautrix_instagram/commands/conn.py

@@ -0,0 +1,55 @@
+# mautrix-twitter - A Matrix-Twitter DM puppeting bridge
+# Copyright (C) 2020 Tulir Asokan
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+from mauigpapi.errors import IGNotLoggedInError
+from mautrix.bridge.commands import HelpSection, command_handler
+from .typehint import CommandEvent
+
+SECTION_CONNECTION = HelpSection("Connection management", 15, "")
+
+
+@command_handler(needs_auth=False, management_only=True, help_section=SECTION_CONNECTION,
+                 help_text="Mark this room as your bridge notice room")
+async def set_notice_room(evt: CommandEvent) -> None:
+    evt.sender.notice_room = evt.room_id
+    await evt.sender.update()
+    await evt.reply("This room has been marked as your bridge notice room")
+
+
+@command_handler(needs_auth=False, management_only=True, help_section=SECTION_CONNECTION,
+                 help_text="Check if you're logged into Instagram")
+async def ping(evt: CommandEvent) -> None:
+    if not await evt.sender.is_logged_in():
+        await evt.reply("You're not logged into Instagram")
+        return
+    try:
+        user_info = await evt.sender.client.current_user()
+    except IGNotLoggedInError:
+        await evt.reply("You have been logged out")
+        await evt.sender.logout()
+    else:
+        user = user_info.user
+        await evt.reply(f"You're logged in as {user.full_name} ([@{user.username}]"
+                        f"(https://instagram.com/{user.username}), user ID: {user.pk})")
+
+
+@command_handler(needs_auth=True, management_only=False, help_section=SECTION_CONNECTION,
+                 help_text="Synchronize portals")
+async def sync(evt: CommandEvent) -> None:
+    await evt.sender.sync()
+    await evt.reply("Synchronization complete")
+
+
+# TODO connect/disconnect MQTT commands

+ 12 - 0
mautrix_instagram/commands/typehint.py

@@ -0,0 +1,12 @@
+from typing import TYPE_CHECKING
+
+from mautrix.bridge.commands import CommandEvent as BaseCommandEvent
+
+if TYPE_CHECKING:
+    from ..__main__ import InstagramBridge
+    from ..user import User
+
+
+class CommandEvent(BaseCommandEvent):
+    bridge: 'InstagramBridge'
+    sender: 'User'