From: Christian Weiske Date: Fri, 6 May 2016 07:07:30 +0000 (+0200) Subject: first working version X-Git-Url: https://git.cweiske.de/errbot-exec.git/commitdiff_plain/cfdaa1aa89972d73b80c52efdf289efd824b51ac first working version --- cfdaa1aa89972d73b80c52efdf289efd824b51ac diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..0d55904 --- /dev/null +++ b/README.rst @@ -0,0 +1,14 @@ +********************** +Exec plugin for errbot +********************** + +Execute an external command when the bot is talked to. + +============= +Configuration +============= +In ``config.py``:: + + EXEC = { + 'command': u'echo' + } diff --git a/exec.plug b/exec.plug new file mode 100644 index 0000000..876b3e1 --- /dev/null +++ b/exec.plug @@ -0,0 +1,14 @@ +[Core] +name = exec +module = exec + +[Documentation] +description = Execute a command when the bot is talked to + +[Python] +version = 2+ + +[Errbot] +min = 4.0.0 +max = 4.0.99 + diff --git a/exec.py b/exec.py new file mode 100644 index 0000000..8efb444 --- /dev/null +++ b/exec.py @@ -0,0 +1,74 @@ +import os +import re +import subprocess +from errbot import BotPlugin, botcmd, re_botcmd +from errbot.utils import ValidationException + +class Exec(BotPlugin): + """ + Execute a command when the bot is talked to + """ + + config_template = { + 'command': u'echo' + } + + def activate(self): + """ + Load configuration from config.by + """ + super(Exec, self).activate() + if (self.config == None and self.bot_config.EXEC): + self.check_configuration(self.bot_config.EXEC) + self.configure(self.bot_config.EXEC) + + def executable_exists(self, name): + """ + Check if an executable exists + """ + if len(name) == 0: + raise ValidationException('Command is empty') + + found = False + for path in os.environ['PATH'].split(os.pathsep): + fullpath = path + os.sep + name + if os.path.exists(fullpath): + found = True + break + if not found: + raise ValidationException('Command not in PATH') + + def get_configuration_template(self): + """ + Defines the configuration structure this plugin supports + """ + return self.config_template + + def check_configuration(self, configuration): + """ + Triggers when the configuration is checked, shortly before activation + """ + super(Exec, self).check_configuration(configuration) + self.executable_exists(configuration['command']) + + @re_botcmd(pattern=r".*", prefixed=False) + def runexec(self, msg, match): + """ + Execute the commmand + """ + for part in ("abc"): + print "part: ", part + try: + output = subprocess.check_output( + [self.config['command'], str(msg), str(msg.frm)], + stderr=subprocess.STDOUT + ) + if len(output) > 0: + return output + else: + return "OK\n" + except subprocess.CalledProcessError as err: + if len(err.output): + return err.output + else: + return "Error"