1 # -*- coding: utf-8 -*-
5 from errbot import BotPlugin, botcmd, re_botcmd
6 from errbot.utils import ValidationException
10 Execute a command when the bot is talked to
19 Load configuration from config.by
21 super(Exec, self).activate()
22 if (self.config == None and self.bot_config.EXEC):
23 self.check_configuration(self.bot_config.EXEC)
24 self.configure(self.bot_config.EXEC)
26 def executable_exists(self, name):
28 Check if an executable exists
31 raise ValidationException('Command is empty')
34 for path in os.environ['PATH'].split(os.pathsep):
35 fullpath = path + os.sep + name
36 if os.path.exists(fullpath):
40 raise ValidationException('Command not in PATH')
42 def get_configuration_template(self):
44 Defines the configuration structure this plugin supports
46 return self.config_template
48 def check_configuration(self, configuration):
50 Triggers when the configuration is checked, shortly before activation
52 super(Exec, self).check_configuration(configuration)
53 self.executable_exists(configuration['command'])
55 @re_botcmd(pattern=r".*", prefixed=False)
56 def runexec(self, msg, match):
61 output = subprocess.check_output(
62 [self.config['command'], unicode(msg.body), unicode(msg.frm)],
63 stderr=subprocess.STDOUT
66 return unicode(output, 'utf-8')
69 except subprocess.CalledProcessError as err:
71 return unicode(err.output, 'utf-8')