comparison src/plugins/plugin_exp_command_export.py @ 604:16a9affc49cf

plugin export command: first draft this plugin allow to export the stdin/stdout/stderr of a process to a contact
author Goffi <goffi@goffi.org>
date Fri, 22 Feb 2013 00:22:50 +0100
parents
children 84a6e83157c2
comparison
equal deleted inserted replaced
603:1d6f48ae31d1 604:16a9affc49cf
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT plugin to export commands (experimental)
6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, warning, error
23 from twisted.words.protocols.jabber import jid
24 from twisted.internet import reactor, protocol
25
26 from sat.tools.misc import SkipOtherTriggers
27 from sat.tools.utils import clean_ustr
28
29 PLUGIN_INFO = {
30 "name": "Command export plugin",
31 "import_name": "EXP-COMMANS-EXPORT",
32 "type": "EXP",
33 "protocols": [],
34 "dependencies": [],
35 "main": "CommandExport",
36 "handler": "no",
37 "description": _("""Implementation of command export""")
38 }
39
40 class ExportCommandProtocol(protocol.ProcessProtocol):
41 """ Try to register an account with prosody """
42
43 def __init__(self, parent, target, options, profile):
44 self.parent = parent
45 self.target = target
46 self.options = options
47 self.profile = profile
48
49 def _clean(self, data):
50 if not data:
51 error ("data should not be empty !")
52 return u""
53 decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None]
54 return clean_ustr(decoded)
55
56 def connectionMade(self):
57 info("connectionMade :)")
58
59 def outReceived(self, data):
60 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile)
61
62 def errReceived(self, data):
63 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile)
64
65 def processEnded(self, reason):
66 info (u"process finished: %d" % (reason.value.exitCode,))
67 self.parent.removeProcess(self.target, self)
68
69 def write(self, message):
70 self.transport.write(message.encode('utf-8'))
71
72 def boolOption(self, key):
73 """ Get boolean value from options
74 @param key: name of the option
75 @return: True if key exists and set to "true" (case insensitive),
76 False in all other cases """
77 value = self.options.get(key, "")
78 return value.lower() == "true"
79
80
81 class CommandExport(object):
82 """Command export plugin: export a command to an entity"""
83 #XXX: This plugin can be potentially dangerous if we don't trust entities linked
84 # this is specially true if we have other triggers.
85
86 def __init__(self, host):
87 info(_("Plugin command export initialization"))
88 self.host = host
89 self.spawned = {} # key = entity
90 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000)
91 host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand)
92
93 def removeProcess(self, entity, process):
94 """ Called when the process is finished
95 @param entity: jid.JID attached to the process
96 @param process: process to remove"""
97 try:
98 processes_set = self.spawned[(entity, process.profile)]
99 processes_set.discard(process)
100 if not processes_set:
101 del(self.spawned[(entity, process.profile)])
102 except ValueError:
103 pass
104
105 def MessageReceivedTrigger(self, message, profile):
106 """ Check if source is linked and repeat message, else do nothing """
107 from_jid = jid.JID(message["from"])
108 spawned_key = (from_jid.userhostJID(), profile)
109 try:
110 body = [e for e in message.elements() if e.name == 'body'][0]
111 except IndexError:
112 warning("Received message without body")
113 return False
114
115 mess_data = unicode(body) + '\n'
116
117 if spawned_key in self.spawned:
118 processes_set = self.spawned[spawned_key]
119 _continue = False
120 exclusive = False
121 for process in processes_set:
122 process.write(mess_data)
123 _continue &= process.boolOption("continue")
124 exclusive |= process.boolOption("exclusive")
125 if exclusive:
126 raise SkipOtherTriggers
127 return _continue
128
129 return True
130
131 def _exportCommand(self, command, args, targets, options, profile_key):
132 """ Export a commands to authorised targets
133 @param command: full path of the command to execute
134 @param args: list of arguments, with command name as first one
135 @param targets: list of allowed entities
136 @param options: export options, a dict which can have the following keys ("true" to set booleans):
137 - exclusive: if set, skip all other triggers
138 - loop: if set, restart the command once terminated #TODO
139 - pty: if set, launch in a pseudo terminal
140 - continue: continue normal MessageReceived handling
141 """
142 profile = self.host.memory.getProfileName(profile_key)
143 if not profile:
144 warning("Unknown profile [%s]" % (profile,))
145 return
146
147 for target in targets:
148 try:
149 _jid = jid.JID(target)
150 if not _jid.user or not _jid.host:
151 raise jid.InvalidFormat
152 _jid = _jid.userhostJID()
153 except (jid.InvalidFormat, RuntimeError):
154 info(u"invalid target ignored: %s" % (target,))
155 continue
156 process_prot = ExportCommandProtocol(self, _jid, options, profile)
157 self.spawned.setdefault((_jid, profile),set()).add(process_prot)
158 reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty'))
159