first working version
authorChristian Weiske <cweiske@cweiske.de>
Fri, 6 May 2016 07:07:30 +0000 (09:07 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 6 May 2016 07:07:30 +0000 (09:07 +0200)
README.rst [new file with mode: 0644]
exec.plug [new file with mode: 0644]
exec.py [new file with mode: 0644]

diff --git a/README.rst b/README.rst
new file mode 100644 (file)
index 0000000..0d55904
--- /dev/null
@@ -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 (file)
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 (file)
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"