1 # -*- coding: utf-8 -*-
2 import os, re, subprocess
3 from errbot import BotPlugin, botcmd, re_botcmd, ValidationException
7 Execute a command when the bot is talked to
16 Load configuration from config.by
18 super(Exec, self).activate()
19 if (self.config == None and self.bot_config.EXEC):
20 self.check_configuration(self.bot_config.EXEC)
21 self.configure(self.bot_config.EXEC)
23 def executable_exists(self, name):
25 Check if an executable exists
28 raise ValidationException('Command is empty')
30 if os.path.exists(name):
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'], msg.body, str(msg.frm)],
63 stderr=subprocess.STDOUT
66 return str(output, 'utf-8')
69 except subprocess.CalledProcessError as err:
71 return str(err.output, 'utf-8')