Support äöü
[errbot-exec.git] / exec.py
1 # -*- coding: utf-8 -*-
2 import os
3 import re
4 import subprocess
5 from errbot import BotPlugin, botcmd, re_botcmd
6 from errbot.utils import ValidationException
7
8 class Exec(BotPlugin):
9     """
10     Execute a command when the bot is talked to
11     """
12
13     config_template = {
14         'command': u'echo'
15     }
16
17     def activate(self):
18         """
19         Load configuration from config.by
20         """
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)
25
26     def executable_exists(self, name):
27         """
28         Check if an executable exists
29         """
30         if len(name) == 0:
31             raise ValidationException('Command is empty')
32
33         found = False
34         for path in os.environ['PATH'].split(os.pathsep):
35             fullpath = path + os.sep + name
36             if os.path.exists(fullpath):
37                 found = True
38                 break
39         if not found:
40             raise ValidationException('Command not in PATH')
41
42     def get_configuration_template(self):
43         """
44         Defines the configuration structure this plugin supports
45         """
46         return self.config_template
47
48     def check_configuration(self, configuration):
49         """
50         Triggers when the configuration is checked, shortly before activation
51         """
52         super(Exec, self).check_configuration(configuration)
53         self.executable_exists(configuration['command'])
54
55     @re_botcmd(pattern=r".*", prefixed=False)
56     def runexec(self, msg, match):
57         """
58         Execute the commmand
59         """
60         try:
61             output = subprocess.check_output(
62                 [self.config['command'], unicode(msg.body), unicode(msg.frm)],
63                 stderr=subprocess.STDOUT
64             )
65             if len(output) > 0:
66                 return unicode(output, 'utf-8')
67             else:
68                 return "OK\n"
69         except subprocess.CalledProcessError as err:
70             if len(err.output):
71                 return unicode(err.output, 'utf-8')
72             else:
73                 return "Error"