diff options
Diffstat (limited to 'inverter-bot')
-rwxr-xr-x | inverter-bot | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/inverter-bot b/inverter-bot index fc6fccc..d16359d 100755 --- a/inverter-bot +++ b/inverter-bot @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import logging, re, datetime, json +import logging, re, datetime, json, subprocess, os from inverterd import Format, Client as InverterClient, InverterError from typing import Optional @@ -144,6 +144,30 @@ def msg_status(update: Update, context: CallbackContext) -> None: handle_exc(update, e) +def msg_shell(update: Update, context: CallbackContext) -> None: + try: + argv = re.findall('^shell (.*)$', update.message.text)[0].split() + result = subprocess.run(argv, capture_output=True) + if result.returncode != 0: + raise ChildProcessError('spawned process returned ' + str(result.returncode)) + + buf = '[stdout] ' + result.stdout.decode('utf-8') + buf += '\n[stderr] ' + result.stderr.decode('utf-8') + reply(update, escape(buf)) + except Exception as e: + logging.exception(str(e)) + reply(update, 'exception: ' + escape(str(e))) + + +def msg_spawn(update: Update, context: CallbackContext) -> None: + try: + argv = re.findall('^spawn (.*)$', update.message.text)[0].split() + os.spawnlp(os.P_NOWAIT, argv[0], *argv) + except Exception as e: + logging.exception(str(e)) + reply(update, 'exception: ' + escape(str(e))) + + def msg_generation(update: Update, context: CallbackContext) -> None: try: today = datetime.date.today() @@ -220,6 +244,7 @@ if __name__ == '__main__': help='ID of users allowed to use the bot') parser.add_argument('--inverterd-host', default='127.0.0.1', type=str) parser.add_argument('--inverterd-port', default=8305, type=int) + parser.add_argument('--shell-admin', required=True, type=int) args = parser.parse_args() whitelist = list(map(lambda x: int(x), args.users_whitelist)) @@ -236,15 +261,21 @@ if __name__ == '__main__': dispatcher = updater.dispatcher user_filter = Filters.user(whitelist) + shell_admin_filter = Filters.user((args.shell_admin, )) dispatcher.add_handler(CommandHandler('start', start)) + dispatcher.add_handler(MessageHandler(Filters.regex(r'^shell ') & shell_admin_filter, msg_shell)) + dispatcher.add_handler(MessageHandler(Filters.regex(r'^spawn ') & shell_admin_filter, msg_spawn)) + dispatcher.add_handler(MessageHandler(Filters.text(_('status')) & user_filter, msg_status)) dispatcher.add_handler(MessageHandler(Filters.text(_('generation')) & user_filter, msg_generation)) dispatcher.add_handler(MessageHandler(Filters.text(_('gs')) & user_filter, msg_gs)) dispatcher.add_handler(MessageHandler(Filters.text(_('ri')) & user_filter, msg_ri)) dispatcher.add_handler(MessageHandler(Filters.text(_('errors')) & user_filter, msg_errors)) + dispatcher.add_handler(MessageHandler(Filters.all & user_filter, msg_all)) + # start the bot updater.start_polling() |