configurator.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Friendly Telegram (telegram userbot)
  2. # Copyright (C) 2018-2021 The Authors
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU Affero General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU Affero General Public License for more details.
  11. # You should have received a copy of the GNU Affero General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. # ©️ Dan Gazizullin, 2021-2023
  14. # This file is a part of Hikka Userbot
  15. # 🌐 https://github.com/hikariatama/Hikka
  16. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  17. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  18. import locale
  19. import os
  20. import string
  21. import sys
  22. import typing
  23. from dialog import Dialog, ExecutableNotFound
  24. from . import utils
  25. def _safe_input(*args, **kwargs):
  26. """Try to invoke input(*), print an error message if an EOFError or OSError occurs)
  27. """
  28. try:
  29. return input(*args, **kwargs)
  30. except (EOFError, OSError):
  31. raise
  32. except KeyboardInterrupt:
  33. print()
  34. return None
  35. class TDialog:
  36. """Reimplementation of dialog.Dialog without external dependencies"""
  37. def inputbox(self, query: str) -> typing.Tuple[bool, str]:
  38. """Get a text input of the query"""
  39. print(query)
  40. print()
  41. inp = _safe_input("Please enter your response, or type nothing to cancel: ")
  42. return (False, "Cancelled") if not inp else (True, inp)
  43. def msgbox(self, msg: str) -> bool:
  44. """Print some info"""
  45. print(msg)
  46. return True
  47. TITLE = ""
  48. if sys.stdout.isatty():
  49. try:
  50. DIALOG = Dialog(dialog="dialog", autowidgetsize=True)
  51. locale.setlocale(locale.LC_ALL, "")
  52. except (ExecutableNotFound, locale.Error):
  53. # Fall back to a terminal based configurator.
  54. DIALOG = TDialog()
  55. else:
  56. DIALOG = TDialog()
  57. def api_config(data_root: str):
  58. """Request API config from user and set"""
  59. code, hash_value = DIALOG.inputbox("Enter your API Hash")
  60. if not code:
  61. return
  62. if len(hash_value) != 32 or any(it not in string.hexdigits for it in hash_value):
  63. DIALOG.msgbox("Invalid hash")
  64. return
  65. code, id_value = DIALOG.inputbox("Enter your API ID")
  66. if not id_value or any(it not in string.digits for it in id_value):
  67. DIALOG.msgbox("Invalid ID")
  68. return
  69. with open(
  70. os.path.join(
  71. data_root or os.path.dirname(utils.get_base_dir()), "api_token.txt"
  72. ),
  73. "w",
  74. ) as file:
  75. file.write(id_value + "\n" + hash_value)
  76. DIALOG.msgbox("API Token and ID set.")