From 6db77ea27caf273438c5583fdd0d0863304180f6 Mon Sep 17 00:00:00 2001 From: moneromooo Date: Mon, 2 Feb 2015 22:14:39 +0000 Subject: [PATCH] Add load and unload commands --- tipbot.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tipbot.py b/tipbot.py index 74bfcf4..285a8a8 100644 --- a/tipbot.py +++ b/tipbot.py @@ -260,6 +260,45 @@ def SendToLink(link,msg): def IsRegistered(link,cmd): RunRegisteredCommand(link,SendToLink,"You are registered",SendToLink,"You are not registered") +def Load(link,cmd): + modulename=GetParam(cmd,1) + if not modulename: + link.send("Usage: load ") + return + if modulename=="builtin": + link.send("Cannot load builtin module") + return + if modulename in sys.modules: + link.send("There is already a %s module" % modulename) + return + log_info('Loading %s module' % modulename) + try: + __import__(modulename) + link.send('%s loaded' % modulename) + except Exception,e: + log_error('Failed to load module "%s": %s' % (modulename, str(e))) + link.send('An error occured') + +def Unload(link,cmd): + modulename=GetParam(cmd,1) + if not modulename: + link.send("Usage: unload ") + return + if modulename=="builtin": + link.send("Cannot unload builtin module") + return + if not modulename in sys.modules: + link.send("%s is not a dynamic module" % modulename) + return + log_info('Unloading %s module' % modulename) + UnregisterModule(modulename) + try: + del sys.modules[modulename] + link.send('%s unloaded' % modulename) + except Exception,e: + log_error('Failed to unload module "%s": %s' % (modulename, str(e))) + link.send('An error occured') + def Reload(link,cmd): modulename=GetParam(cmd,1) if not modulename: @@ -320,6 +359,8 @@ def RegisterCommands(): RegisterCommand({'module': 'builtin', 'name': 'scanwho', 'function': ScanWho, 'admin': True, 'help': "Refresh users list in a channel"}) RegisterCommand({'module': 'builtin', 'name': 'dump_users', 'function': DumpUsers, 'admin': True, 'help': "Dump users table to log"}) RegisterCommand({'module': 'builtin', 'name': 'show_activity', 'function': ShowActivity, 'admin': True, 'help': "Show time since a user was last active"}) + RegisterCommand({'module': 'builtin', 'name': 'load', 'function': Load, 'admin': True, 'help': "Load a module"}) + RegisterCommand({'module': 'builtin', 'name': 'unload', 'function': Unload, 'admin': True, 'help': "Unload a module"}) RegisterCommand({'module': 'builtin', 'name': 'reload', 'function': Reload, 'admin': True, 'help': "Reload a module"}) RegisterCommand({'module': 'builtin', 'name': 'disable', 'function': Disable, 'admin': True, 'help': "Disable %s"%config.tipbot_name}) RegisterCommand({'module': 'builtin', 'name': 'quit', 'function': Quit, 'admin': True, 'help': "Quit"})