configurator.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  14. # █▀█ █ █ █ █▀█ █▀▄ █
  15. # © Copyright 2022
  16. # https://t.me/hikariatama
  17. #
  18. # 🔒 Licensed under the GNU AGPLv3
  19. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  20. import locale
  21. import os
  22. import string
  23. import sys
  24. import typing
  25. from dialog import Dialog, ExecutableNotFound
  26. from . import utils
  27. def _safe_input(*args, **kwargs):
  28. """Try to invoke input(*), print an error message if an EOFError or OSError occurs)
  29. """
  30. try:
  31. return input(*args, **kwargs)
  32. except (EOFError, OSError):
  33. raise
  34. except KeyboardInterrupt:
  35. print()
  36. return None
  37. class TDialog:
  38. """Reimplementation of dialog.Dialog without external dependencies"""
  39. def inputbox(self, query: str) -> typing.Tuple[bool, str]:
  40. """Get a text input of the query"""
  41. print(query)
  42. print()
  43. inp = _safe_input("Please enter your response, or type nothing to cancel: ")
  44. return (False, "Cancelled") if not inp else (True, inp)
  45. def msgbox(self, msg: str) -> bool:
  46. """Print some info"""
  47. print(msg)
  48. return True
  49. TITLE = ""
  50. if sys.stdout.isatty():
  51. try:
  52. DIALOG = Dialog(dialog="dialog", autowidgetsize=True)
  53. locale.setlocale(locale.LC_ALL, "")
  54. except (ExecutableNotFound, locale.Error):
  55. # Fall back to a terminal based configurator.
  56. DIALOG = TDialog()
  57. else:
  58. DIALOG = TDialog()
  59. def api_config(data_root: str):
  60. """Request API config from user and set"""
  61. code, hash_value = DIALOG.inputbox("Enter your API Hash")
  62. if code == DIALOG.OK:
  63. if len(hash_value) != 32 or any(
  64. it not in string.hexdigits for it in hash_value
  65. ):
  66. DIALOG.msgbox("Invalid hash")
  67. return
  68. code, id_value = DIALOG.inputbox("Enter your API ID")
  69. if not id_value or any(it not in string.digits for it in id_value):
  70. DIALOG.msgbox("Invalid ID")
  71. return
  72. with open(
  73. os.path.join(
  74. data_root or os.path.dirname(utils.get_base_dir()), "api_token.txt"
  75. ),
  76. "w",
  77. ) as file:
  78. file.write(id_value + "\n" + hash_value)
  79. DIALOG.msgbox("API Token and ID set.")