conn.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # mautrix-twitter - A Matrix-Twitter DM 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 mauigpapi.errors import IGNotLoggedInError
  17. from mautrix.bridge.commands import HelpSection, command_handler
  18. from .typehint import CommandEvent
  19. SECTION_CONNECTION = HelpSection("Connection management", 15, "")
  20. @command_handler(needs_auth=False, management_only=True, help_section=SECTION_CONNECTION,
  21. help_text="Mark this room as your bridge notice room")
  22. async def set_notice_room(evt: CommandEvent) -> None:
  23. evt.sender.notice_room = evt.room_id
  24. await evt.sender.update()
  25. await evt.reply("This room has been marked as your bridge notice room")
  26. @command_handler(needs_auth=False, management_only=True, help_section=SECTION_CONNECTION,
  27. help_text="Check if you're logged into Instagram")
  28. async def ping(evt: CommandEvent) -> None:
  29. if not await evt.sender.is_logged_in():
  30. await evt.reply("You're not logged into Instagram")
  31. return
  32. try:
  33. user_info = await evt.sender.client.current_user()
  34. except IGNotLoggedInError as e:
  35. # TODO maybe don't always log out?
  36. evt.log.exception(f"Got error checking current user for %s, logging out. %s",
  37. evt.sender.mxid, e.body.json())
  38. await evt.reply("You have been logged out")
  39. await evt.sender.logout()
  40. else:
  41. user = user_info.user
  42. await evt.reply(f"You're logged in as {user.full_name} ([@{user.username}]"
  43. f"(https://instagram.com/{user.username}), user ID: {user.pk})")
  44. if evt.sender.is_connected:
  45. await evt.reply("MQTT connection is active")
  46. elif evt.sender.mqtt and evt.sender._listen_task:
  47. await evt.reply("MQTT connection is reconnecting")
  48. else:
  49. await evt.reply("MQTT not connected")
  50. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_CONNECTION,
  51. help_text="Synchronize portals")
  52. async def sync(evt: CommandEvent) -> None:
  53. await evt.sender.sync()
  54. await evt.reply("Synchronization complete")
  55. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_CONNECTION,
  56. help_text="Connect to Instagram", aliases=["reconnect"])
  57. async def connect(evt: CommandEvent) -> None:
  58. if evt.sender.is_connected:
  59. await evt.reply("You're already connected to Instagram.")
  60. return
  61. # TODO backfill when reconnecting
  62. await evt.sender.stop_listen()
  63. evt.sender.shutdown = False
  64. await evt.sender.start_listen()
  65. await evt.reply("Restarted connection to Instagram.")
  66. @command_handler(needs_auth=True, management_only=False, help_section=SECTION_CONNECTION,
  67. help_text="Disconnect from Instagram")
  68. async def disconnect(evt: CommandEvent) -> None:
  69. if not evt.sender.mqtt:
  70. await evt.reply("You're not connected to Instagram.")
  71. await evt.sender.stop_listen()
  72. evt.sender.shutdown = False
  73. await evt.reply("Successfully disconnected from Instagram.")