__main__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Entry point. Checks for user and starts main script"""
  2. # Friendly Telegram (telegram userbot)
  3. # Copyright (C) 2018-2021 The Authors
  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. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Affero General Public License for more details.
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  15. # █▀█ █ █ █ █▀█ █▀▄ █
  16. # © Copyright 2022
  17. # https://t.me/hikariatama
  18. #
  19. # 🔒 Licensed under the GNU AGPLv3
  20. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  21. import atexit
  22. import getpass
  23. import os
  24. import subprocess
  25. import sys
  26. if (
  27. getpass.getuser() == "root"
  28. and "--root" not in " ".join(sys.argv)
  29. and "OKTETO" not in os.environ
  30. and "DOCKER" not in os.environ
  31. ):
  32. print("🚫" * 15)
  33. print("You attempted to run Hikka on behalf of root user")
  34. print("Please, create a new user and restart script")
  35. print("If this action was intentional, pass --root argument instead")
  36. print("🚫" * 15)
  37. print()
  38. print("Type force_insecure to ignore this warning")
  39. if input("> ").lower() != "force_insecure":
  40. sys.exit(1)
  41. def deps(error):
  42. print(
  43. "🚫 Error: you have not installed all dependencies correctly.\n"
  44. f"{str(error)}\n"
  45. "🔄 Attempting dependencies installation... Just wait ⏱"
  46. )
  47. subprocess.run(
  48. [
  49. sys.executable,
  50. "-m",
  51. "pip",
  52. "install",
  53. "--upgrade",
  54. "-q",
  55. "--disable-pip-version-check",
  56. "--no-warn-script-location",
  57. "-r",
  58. "requirements.txt",
  59. ],
  60. check=True,
  61. )
  62. restart()
  63. def restart():
  64. if "HIKKA_DO_NOT_RESTART" in os.environ:
  65. print("Got in a loop, exiting")
  66. sys.exit(0)
  67. print("🔄 Restarting...")
  68. atexit.register(
  69. lambda: os.execl(
  70. sys.executable,
  71. sys.executable,
  72. "-m",
  73. os.path.relpath(
  74. os.path.abspath(
  75. os.path.dirname(
  76. os.path.abspath(__file__),
  77. ),
  78. ),
  79. ),
  80. *(sys.argv[1:]),
  81. )
  82. )
  83. os.environ["HIKKA_DO_NOT_RESTART"] = "1"
  84. sys.exit(0)
  85. if sys.version_info < (3, 8, 0):
  86. print("🚫 Error: you must use at least Python version 3.8.0")
  87. elif __package__ != "hikka": # In case they did python __main__.py
  88. print("🚫 Error: you cannot run this as a script; you must execute as a package")
  89. else:
  90. try:
  91. import telethon # noqa: F401
  92. except Exception:
  93. pass
  94. else:
  95. try:
  96. # This is used as verification markers to ensure that supported
  97. # version is installed
  98. from telethon.tl.types import MessageEntityCustomEmoji # noqa: F401
  99. from telethon.extensions.html import CUSTOM_EMOJIS # noqa: F401
  100. except ImportError:
  101. print(
  102. "⚠️ Warning: Classic telethon is used as main one. This can cause"
  103. " errors and enables DAR. Attempting to reinstall custom hikka"
  104. " telethon..."
  105. )
  106. subprocess.run(
  107. [
  108. sys.executable,
  109. "-m",
  110. "pip",
  111. "uninstall",
  112. "-y",
  113. "telethon",
  114. ],
  115. check=True,
  116. )
  117. subprocess.run(
  118. [
  119. sys.executable,
  120. "-m",
  121. "pip",
  122. "install",
  123. "--force-reinstall",
  124. "-q",
  125. "--disable-pip-version-check",
  126. "--no-warn-script-location",
  127. "git+https://github.com/hikariatama/Telethon",
  128. ],
  129. check=True,
  130. )
  131. restart()
  132. try:
  133. from . import log
  134. log.init()
  135. from . import main
  136. except ModuleNotFoundError as e: # pragma: no cover
  137. deps(e)
  138. if __name__ == "__main__":
  139. if "HIKKA_DO_NOT_RESTART" in os.environ:
  140. del os.environ["HIKKA_DO_NOT_RESTART"]
  141. main.hikka.main() # Execute main function