conn.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2022 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(
  21. needs_auth=False,
  22. management_only=True,
  23. help_section=SECTION_CONNECTION,
  24. help_text="Mark this room as your bridge notice room",
  25. )
  26. async def set_notice_room(evt: CommandEvent) -> None:
  27. evt.sender.notice_room = evt.room_id
  28. await evt.sender.update()
  29. await evt.reply("This room has been marked as your bridge notice room")
  30. @command_handler(
  31. needs_auth=False,
  32. management_only=True,
  33. help_section=SECTION_CONNECTION,
  34. help_text="Check if you're logged into Instagram",
  35. )
  36. async def ping(evt: CommandEvent) -> None:
  37. if not await evt.sender.is_logged_in():
  38. await evt.reply("You're not logged into Instagram")
  39. return
  40. try:
  41. user_info = await evt.sender.client.current_user()
  42. except IGNotLoggedInError as e:
  43. evt.log.exception("Got error checking current user for %s", evt.sender.mxid)
  44. await evt.reply("You have been logged out")
  45. await evt.sender.logout(error=e)
  46. else:
  47. user = user_info.user
  48. await evt.reply(
  49. f"You're logged in as {user.full_name} ([@{user.username}]"
  50. f"(https://instagram.com/{user.username}), user ID: {user.pk})"
  51. )
  52. if evt.sender.is_connected:
  53. await evt.reply("MQTT connection is active")
  54. elif evt.sender.mqtt and evt.sender._listen_task:
  55. await evt.reply("MQTT connection is reconnecting")
  56. else:
  57. await evt.reply("MQTT not connected")
  58. @command_handler(
  59. needs_auth=True,
  60. management_only=False,
  61. help_section=SECTION_CONNECTION,
  62. help_text="Reconnect to Instagram and synchronize portals",
  63. aliases=["sync"],
  64. )
  65. async def refresh(evt: CommandEvent) -> None:
  66. await evt.sender.refresh()
  67. await evt.reply("Synchronization complete")
  68. @command_handler(
  69. needs_auth=True,
  70. management_only=False,
  71. help_section=SECTION_CONNECTION,
  72. help_text="Connect to Instagram",
  73. aliases=["reconnect"],
  74. )
  75. async def connect(evt: CommandEvent) -> None:
  76. if evt.sender.is_connected:
  77. await evt.reply("You're already connected to Instagram.")
  78. return
  79. await evt.sender.refresh(resync=False)
  80. await evt.reply("Restarted connection to Instagram.")
  81. @command_handler(
  82. needs_auth=True,
  83. management_only=False,
  84. help_section=SECTION_CONNECTION,
  85. help_text="Disconnect from Instagram",
  86. )
  87. async def disconnect(evt: CommandEvent) -> None:
  88. if not evt.sender.mqtt:
  89. await evt.reply("You're not connected to Instagram.")
  90. await evt.sender.stop_listen()
  91. await evt.reply("Successfully disconnected from Instagram.")