def unload(update: Update, context: CallbackContext): if not user_is_admin(update.message.from_user.id): update.message.reply_text("Error: You are not authorized to unload modules") return if len(context.args) < 1: update.message.reply_text("Error: Module name not provided") return result = {} for module_name in context.args: if module_name == "core": text = "Error: You can't unload module used for loading/unloading modules" else: try: context.dispatcher.unload_module(module_name) except ModuleNotFoundError: text = "Error: Module not found" except AttributeError: text = "Module already unloaded" else: text = "Module unloaded" result[module_name] = text text = "" for module_name, status in result.items(): text += f"{module_name}: {status}\n" update.message.reply_text(text)
def shell(update: Update, context: CallbackContext): if not user_is_admin(update.message.from_user.id): update.message.reply_text( "Error: You are not authorized to load modules") return if len(update.message.text.split(' ', 1)) < 2: update.message.reply_text("No command provided") return command = update.message.text.split(' ', 1)[1] try: process = subprocess.check_output(command, shell=True, executable="/bin/bash", stderr=subprocess.STDOUT, universal_newlines=True) except subprocess.CalledProcessError as e: returncode = e.returncode output = e.output else: returncode = 0 output = process update.message.reply_text(f"Command: {command}\n" f"Return code: {returncode}\n\n" f"Output:\n" f"{output}\n")
def disable(update: Update, context: CallbackContext): if not user_is_admin(update.message.from_user.id): update.message.reply_text( "Error: You are not authorized to load modules") return if len(context.args) < 1: update.message.reply_text("Error: Module name not provided") return result = {} for module_name in context.args: module = get_module(module_name) if module is None: result[module_name] = "Module not found" continue if module.type == MODULE_TYPE_CORE: result[module_name] = "You can't disable a core module" continue try: context.dispatcher.disable_module(module_name) except AttributeError: result[module_name] = "Module already disabled" continue result[module_name] = "Module disabled" text = [ f"{module_name}: {status}" for module_name, status in result.items() ] update.message.reply_text("\n".join(text))
def ci(update: Update, context: CallbackContext): if not user_is_admin(update.message.from_user.id): update.message.reply_text( "Error: You are not authorized to use CI function of this bot.\n" "Ask to who host this bot to add you to the authorized people list" ) return if get_config("ci.channel_id") is None: update.message.reply_text( "Error: CI channel or user ID not defined") LOGE("CI channel or user ID not defined") return parser = CIParser(prog="/ci") parser.set_output(update.message.reply_text) parser.add_argument( 'project', help='CI project', nargs='?', default=None, ) parser.add_argument('-s', '--status', action='store_true', help='show queue status') args, project_args = parser.parse_known_args(context.args) if args.status: update.message.reply_text(queue_manager.get_formatted_queue_list()) return if args.project is None: parser.error("Please specify a project") try: project_class = import_module( f"homebot.modules.ci.projects.{args.project}", package="Project").Project except (ModuleNotFoundError, AttributeError): update.message.reply_text("Error: Project script not found") return except Exception as e: text = "Error: Error while importing project:" text += format_exception(e) update.message.reply_text(text) LOGE(text) return try: project = project_class(update, context, project_args) except Exception as e: text = "Error: Project class initialization failed:\n" text += format_exception(e) update.message.reply_text(text) LOGE(text) return workflow = Workflow(project) queue_manager.put(workflow) update.message.reply_text("Workflow added to the queue") LOGI("Workflow added to the queue")