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