Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_command_export.py @ 2562:26edcf3a30eb
core, setup: huge cleaning:
- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention
- move twisted directory to root
- removed all hacks from setup.py, and added missing dependencies, it is now clean
- use https URL for website in setup.py
- removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed
- renamed sat.sh to sat and fixed its installation
- added python_requires to specify Python version needed
- replaced glib2reactor which use deprecated code by gtk3reactor
sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Apr 2018 19:44:50 +0200 |
parents | src/plugins/plugin_exp_command_export.py@0046283a285d |
children | 56f94936df1e |
comparison
equal
deleted
inserted
replaced
2561:bd30dc3ffe5a | 2562:26edcf3a30eb |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin to export commands (experimental) | |
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core.constants import Const as C | |
22 from sat.core.log import getLogger | |
23 log = getLogger(__name__) | |
24 from twisted.words.protocols.jabber import jid | |
25 from twisted.internet import reactor, protocol | |
26 | |
27 from sat.tools import trigger | |
28 from sat.tools.utils import clean_ustr | |
29 | |
30 PLUGIN_INFO = { | |
31 C.PI_NAME: "Command export plugin", | |
32 C.PI_IMPORT_NAME: "EXP-COMMANS-EXPORT", | |
33 C.PI_TYPE: "EXP", | |
34 C.PI_PROTOCOLS: [], | |
35 C.PI_DEPENDENCIES: [], | |
36 C.PI_MAIN: "CommandExport", | |
37 C.PI_HANDLER: "no", | |
38 C.PI_DESCRIPTION: _("""Implementation of command export""") | |
39 } | |
40 | |
41 class ExportCommandProtocol(protocol.ProcessProtocol): | |
42 """ Try to register an account with prosody """ | |
43 | |
44 def __init__(self, parent, client, target, options): | |
45 self.parent = parent | |
46 self.target = target | |
47 self.options = options | |
48 self.client = client | |
49 | |
50 def _clean(self, data): | |
51 if not data: | |
52 log.error ("data should not be empty !") | |
53 return u"" | |
54 decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None] | |
55 return clean_ustr(decoded) | |
56 | |
57 def connectionMade(self): | |
58 log.info("connectionMade :)") | |
59 | |
60 def outReceived(self, data): | |
61 self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True) | |
62 | |
63 def errReceived(self, data): | |
64 self.client.sendMessage(self.target, {'': self._clean(data)}, no_trigger=True) | |
65 | |
66 def processEnded(self, reason): | |
67 log.info (u"process finished: %d" % (reason.value.exitCode,)) | |
68 self.parent.removeProcess(self.target, self) | |
69 | |
70 def write(self, message): | |
71 self.transport.write(message.encode('utf-8')) | |
72 | |
73 def boolOption(self, key): | |
74 """ Get boolean value from options | |
75 @param key: name of the option | |
76 @return: True if key exists and set to "true" (case insensitive), | |
77 False in all other cases """ | |
78 value = self.options.get(key, "") | |
79 return value.lower() == "true" | |
80 | |
81 | |
82 class CommandExport(object): | |
83 """Command export plugin: export a command to an entity""" | |
84 # XXX: This plugin can be potentially dangerous if we don't trust entities linked | |
85 # this is specially true if we have other triggers. | |
86 # FIXME: spawned should be a client attribute, not a class one | |
87 | |
88 def __init__(self, host): | |
89 log.info(_("Plugin command export initialization")) | |
90 self.host = host | |
91 self.spawned = {} # key = entity | |
92 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000) | |
93 host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand) | |
94 | |
95 def removeProcess(self, entity, process): | |
96 """ Called when the process is finished | |
97 @param entity: jid.JID attached to the process | |
98 @param process: process to remove""" | |
99 try: | |
100 processes_set = self.spawned[(entity, process.client.profile)] | |
101 processes_set.discard(process) | |
102 if not processes_set: | |
103 del(self.spawned[(entity, process.client.profile)]) | |
104 except ValueError: | |
105 pass | |
106 | |
107 def MessageReceivedTrigger(self, client, message_elt, post_treat): | |
108 """ Check if source is linked and repeat message, else do nothing """ | |
109 from_jid = jid.JID(message_elt["from"]) | |
110 spawned_key = (from_jid.userhostJID(), client.profile) | |
111 | |
112 if spawned_key in self.spawned: | |
113 try: | |
114 body = message_elt.elements(C.NS_CLIENT, 'body').next() | |
115 except StopIteration: | |
116 # do not block message without body (chat state notification...) | |
117 return True | |
118 | |
119 mess_data = unicode(body) + '\n' | |
120 processes_set = self.spawned[spawned_key] | |
121 _continue = False | |
122 exclusive = False | |
123 for process in processes_set: | |
124 process.write(mess_data) | |
125 _continue &= process.boolOption("continue") | |
126 exclusive |= process.boolOption("exclusive") | |
127 if exclusive: | |
128 raise trigger.SkipOtherTriggers | |
129 return _continue | |
130 | |
131 return True | |
132 | |
133 def _exportCommand(self, command, args, targets, options, profile_key): | |
134 """ Export a commands to authorised targets | |
135 @param command: full path of the command to execute | |
136 @param args: list of arguments, with command name as first one | |
137 @param targets: list of allowed entities | |
138 @param options: export options, a dict which can have the following keys ("true" to set booleans): | |
139 - exclusive: if set, skip all other triggers | |
140 - loop: if set, restart the command once terminated #TODO | |
141 - pty: if set, launch in a pseudo terminal | |
142 - continue: continue normal MessageReceived handling | |
143 """ | |
144 client = self.host.getClient(profile_key) | |
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 (RuntimeError, jid.InvalidFormat, AttributeError): | |
152 log.info(u"invalid target ignored: %s" % (target,)) | |
153 continue | |
154 process_prot = ExportCommandProtocol(self, client, _jid, options) | |
155 self.spawned.setdefault((_jid, client.profile),set()).add(process_prot) | |
156 reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty')) |