Exemple #1
0
 def _edit_script_menu_new_script(self, update, context):
     delete_callback_message(update, context)
     if self._check_permission(update):
         context.chat_data["current_script"] = Script(
             "script name", "cd", "script description", True)
         self._edit_script_menu_show_script(update, context)
         return self.EDIT_SCRIPT
     send_message(update, context, self._res.ACCESS_DENIED_TEXT)
     return ConversationHandler.END
Exemple #2
0
 def _remove_script(self, update, context):
     delete_callback_message(update, context)
     script_name = context.chat_data["argument"]
     script = Script.get_script_from_name(self._scripts, script_name)
     self._scripts.remove(script)
     send_message(update,
                  context,
                  text=self._res.SCRIPT_SUCCESSFULLY_REMOVED_TEXT.format(
                      script_name))
     save_scripts_json(self._scripts)
Exemple #3
0
 def _remove_script_handler(self, update, context):
     delete_callback_message(update, context)
     if self._check_permission(update):
         send_message(update,
                      context,
                      text=str(
                          Script.get_script_from_name(
                              self._scripts, update.callback_query.data)),
                      parse_mode=ParseMode.MARKDOWN)
         self._send_confirmation(update,
                                 context,
                                 self._remove_script,
                                 argument=update.callback_query.data,
                                 current_menu=self._options_command_handler)
     else:
         send_message(update, context, self._res.ACCESS_DENIED_TEXT)
         return ConversationHandler.END
Exemple #4
0
 def _handle_script(self, update, context):
     """ handle the execution of a script """
     if self._check_permission(update):
         script = Script.get_script_from_name(self._scripts,
                                              update.callback_query.data)
         if script.verbose:
             for command in script.body.split("\n"):
                 self._handle_shell_input(update, context, command)
         else:
             for command in script.body.split("\n"):
                 self.shell.execute_command(command)
         send_message(update, context,
                      self._res.SCRIPT_DONE_TEXT.format(script.name))
     else:
         send_message(update, context, self._res.ACCESS_DENIED_TEXT)
     delete_callback_message(update, context)
     return ConversationHandler.END
Exemple #5
0
def save_scripts_json(script_list):
    save_json_to_file(Script.get_script_list_json(script_list), "scripts.json")
Exemple #6
0
 def _edit_script_menu_load_script(self, update, context):
     context.chat_data["current_script"] = Script.get_script_from_name(
         self._scripts, update.callback_query.data)
     return self._edit_script_menu_show_script(update, context)
Exemple #7
0
 def __init__(self, config, scripts_json):
     """ initialize all of the bot's variables """
     # init resources
     self._res = Resources()
     # init telegram stuff
     self._bot_token = config["telegram_bot_token"]
     whitelist = config["whitelist"]
     self._updater = Updater(self._bot_token, use_context=True)
     # conversation handlers
     yes_handler = CallbackQueryHandler(self._action_confirm_handler,
                                        pattern="yes")
     no_handler = CallbackQueryHandler(self._action_cancelled_handler,
                                       pattern="no")
     # options
     self._updater.dispatcher.add_handler(
         ConversationHandler(
             entry_points=[
                 CommandHandler('options', self._options_command_handler)
             ],
             states={
                 self.OPTIONS: [
                     CallbackQueryHandler(
                         self._add_to_whitelist_callback_handler,
                         pattern="addToWhitelist"),
                     CallbackQueryHandler(
                         self._remove_from_whitelist_callback_handler,
                         pattern="removeFromWhitelist"),
                     CallbackQueryHandler(
                         self._show_scripts_callback_handler,
                         pattern="seeAllScripts"),
                     CallbackQueryHandler(
                         self._remove_script_callback_handler,
                         pattern="removeScript"),
                     CallbackQueryHandler(self._edit_script_menu_new_script,
                                          pattern="newScript"),
                     CallbackQueryHandler(
                         self._edit_script_menu_load_script_handler,
                         pattern="editScript"),
                     CallbackQueryHandler(self._end_conversation_handler,
                                          pattern="back")
                 ],
                 self.ADD_USER: [
                     yes_handler, no_handler,
                     MessageHandler(Filters.text & (~Filters.command),
                                    self._add_to_whitelist_handler)
                 ],
                 self.REMOVE_USER: [
                     yes_handler,
                     no_handler,
                     CallbackQueryHandler(
                         self._remove_from_whitelist_handler),
                 ],
                 self.REMOVE_SCRIPT: [
                     yes_handler,
                     no_handler,
                     CallbackQueryHandler(self._remove_script_handler),
                 ],
                 self.LOAD_SCRIPT:
                 [CallbackQueryHandler(self._edit_script_menu_load_script)],
                 self.EDIT_SCRIPT: [
                     CallbackQueryHandler(
                         self._toggle_script_verbose_handler,
                         pattern="toggleVerbose"),
                     CallbackQueryHandler(self._edit_script_save,
                                          pattern="save"),
                     CallbackQueryHandler(self._edit_script_back,
                                          pattern="back"),
                     CallbackQueryHandler(
                         self._edit_script_text_field_handler)
                 ],
                 self.EDIT_SCRIPT_TEXTFIELD: [
                     MessageHandler(Filters.text & (~Filters.command),
                                    self._edit_script_text_field)
                 ]
             },
             fallbacks=[
                 CommandHandler("end", self._end_conversation_handler)
             ]))
     # download
     self._updater.dispatcher.add_handler(
         ConversationHandler(
             entry_points=[
                 CommandHandler('download', self._download_command_handler)
             ],
             states={
                 self.DOWNLOAD: [
                     MessageHandler(Filters.text & (~Filters.command),
                                    self._download_handler)
                 ]
             },
             fallbacks=[
                 CommandHandler("end", self._end_conversation_handler)
             ]))
     # upload
     self._updater.dispatcher.add_handler(
         ConversationHandler(
             entry_points=[
                 CommandHandler('upload', self._upload_command_handler)
             ],
             states={
                 self.UPLOAD: [
                     MessageHandler(
                         Filters.all & (~Filters.command) & (~Filters.text),
                         self._upload_handler)
                 ]
             },
             fallbacks=[
                 CommandHandler("end", self._end_conversation_handler)
             ]))
     # scripts
     self._updater.dispatcher.add_handler(
         ConversationHandler(
             entry_points=[
                 CommandHandler('scripts', self._scripts_command_handler)
             ],
             states={
                 self.SCRIPT_SELECT:
                 [CallbackQueryHandler(self._handle_script)]
             },
             fallbacks=[
                 CommandHandler("end", self._end_conversation_handler)
             ]))
     # command handlers
     self._updater.dispatcher.add_handler(
         CommandHandler('start', self._start_command_handler))
     self._updater.dispatcher.add_handler(
         CommandHandler('help', self._help_command_handler))
     self._updater.dispatcher.add_handler(
         CommandHandler('h', self._help_command_handler))
     # text message handler
     self._updater.dispatcher.add_handler(
         MessageHandler(Filters.text & (~Filters.command),
                        self._handle_command))
     # callback query handlers
     self._updater.dispatcher.add_error_handler(self._error)
     # check if there is a whitelist
     if len(whitelist) != 0:
         self._whiteList = whitelist
     else:
         self._whiteList = None
     self._scripts_json = scripts_json
     self._scripts = Script.get_scripts_from_json(self._scripts_json)
     # init shell
     self.shell = Shell(self.WORKING_DIRECTORY)
     print("working directory: " + self.WORKING_DIRECTORY)