forbid_joins.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  2. # █▀█ █ █ █ █▀█ █▀▄ █
  3. # © Copyright 2022
  4. # https://t.me/hikariatama
  5. #
  6. # 🔒 Licensed under the GNU AGPLv3
  7. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  8. from telethon import TelegramClient
  9. from telethon.utils import is_list_like
  10. import inspect
  11. import logging
  12. from . import loader
  13. logger = logging.getLogger(__name__)
  14. # ⚠️⚠️ WARNING! ⚠️⚠️
  15. # If you are a module developer, and you'll try to bypass this protection to
  16. # force user join your channel, you will be added to SCAM modules
  17. # list and you will be banned from Hikka federation.
  18. # Let USER decide, which channel he will follow. Do not be so petty
  19. # I hope, you understood me.
  20. # Thank you
  21. def install_join_forbidder(client: TelegramClient) -> TelegramClient:
  22. if hasattr(client, "_forbid_join_tag"):
  23. return client
  24. old_call = client._call
  25. async def new_call(
  26. sender: "MTProtoSender", # type: ignore
  27. request: "TLRequest", # type: ignore
  28. ordered: bool = False,
  29. flood_sleep_threshold: int = None,
  30. ):
  31. not_tuple = False
  32. if not is_list_like(request):
  33. not_tuple = True
  34. request = (request,)
  35. new_request = []
  36. for item in request:
  37. if item.CONSTRUCTOR_ID in {615851205, 1817183516} and next(
  38. (
  39. frame_info.frame.f_locals["self"]
  40. for frame_info in inspect.stack()
  41. if hasattr(frame_info, "frame")
  42. and hasattr(frame_info.frame, "f_locals")
  43. and isinstance(frame_info.frame.f_locals, dict)
  44. and "self" in frame_info.frame.f_locals
  45. and isinstance(frame_info.frame.f_locals["self"], loader.Module)
  46. and frame_info.frame.f_locals["self"].__class__.__name__
  47. not in {
  48. "APIRatelimiterMod",
  49. "ForbidJoinMod",
  50. "LoaderMod",
  51. "HikkaSettingsMod",
  52. }
  53. # APIRatelimiterMod is a core proxy, so it wraps around every module in Hikka, if installed
  54. # ForbidJoinMod is also a Core proxy, so it wraps around every module in Hikka, if installed
  55. # LoaderMod prompts user to join developers' channels
  56. # HikkaSettings prompts user to join channels, required by modules
  57. ),
  58. None,
  59. ):
  60. logger.debug(
  61. "🎉 I protected you from unintented"
  62. f" {item.__class__.__name__} ({item})!"
  63. )
  64. continue
  65. new_request += [item]
  66. if not new_request:
  67. return
  68. return await old_call(
  69. sender,
  70. new_request[0] if not_tuple else tuple(new_request),
  71. ordered,
  72. flood_sleep_threshold,
  73. )
  74. client._call = new_call
  75. client._joins_forbidden = True
  76. logger.debug("🎉 JoinForbidder installed!")
  77. return client