1 # -*- coding: utf-8 -*-
2 import os, re, subprocess
3 from errbot import BotPlugin, botcmd, re_botcmd
4 from errbot.utils import ValidationException
8 Execute a command when the bot is talked to
17 Load configuration from config.by
19 super(Exec, self).activate()
20 if (self.config == None and self.bot_config.EXEC):
21 self.check_configuration(self.bot_config.EXEC)
22 self.configure(self.bot_config.EXEC)
24 def executable_exists(self, name):
26 Check if an executable exists
29 raise ValidationException('Command is empty')
31 if os.path.exists(name):
35 for path in os.environ['PATH'].split(os.pathsep):
36 fullpath = path + os.sep + name
37 if os.path.exists(fullpath):
41 raise ValidationException('Command not in PATH')
43 def get_configuration_template(self):
45 Defines the configuration structure this plugin supports
47 return self.config_template
49 def check_configuration(self, configuration):
51 Triggers when the configuration is checked, shortly before activation
53 super(Exec, self).check_configuration(configuration)
54 self.executable_exists(configuration['command'])
56 @re_botcmd(pattern=r".*", prefixed=False)
57 def runexec(self, msg, match):
62 output = subprocess.check_output(
63 [self.config['command'], unicode(msg.body), unicode(msg.frm)],
64 stderr=subprocess.STDOUT
67 return unicode(output, 'utf-8')
70 except subprocess.CalledProcessError as err:
72 return unicode(err.output, 'utf-8')