Mercurial > libervia-backend
annotate src/plugins/plugin_exp_command_export.py @ 933:955e5c781a40
core: presence stanzas were sent twice
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 24 Mar 2014 10:16:07 +0100 |
parents | 1fe00f0c9a91 |
children | 301b342c697a |
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) |
811 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 |
771 | 20 from sat.core.i18n import _ |
604 | 21 from logging import debug, info, warning, error |
22 from twisted.words.protocols.jabber import jid | |
23 from twisted.internet import reactor, protocol | |
24 | |
25 from sat.tools.misc import SkipOtherTriggers | |
26 from sat.tools.utils import clean_ustr | |
27 | |
28 PLUGIN_INFO = { | |
29 "name": "Command export plugin", | |
30 "import_name": "EXP-COMMANS-EXPORT", | |
31 "type": "EXP", | |
32 "protocols": [], | |
33 "dependencies": [], | |
34 "main": "CommandExport", | |
35 "handler": "no", | |
36 "description": _("""Implementation of command export""") | |
37 } | |
38 | |
39 class ExportCommandProtocol(protocol.ProcessProtocol): | |
40 """ Try to register an account with prosody """ | |
41 | |
42 def __init__(self, parent, target, options, profile): | |
43 self.parent = parent | |
44 self.target = target | |
45 self.options = options | |
46 self.profile = profile | |
47 | |
48 def _clean(self, data): | |
49 if not data: | |
50 error ("data should not be empty !") | |
51 return u"" | |
52 decoded = data.decode('utf-8', 'ignore')[:-1 if data[-1] == '\n' else None] | |
53 return clean_ustr(decoded) | |
54 | |
55 def connectionMade(self): | |
56 info("connectionMade :)") | |
57 | |
58 def outReceived(self, data): | |
59 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) | |
60 | |
61 def errReceived(self, data): | |
62 self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) | |
63 | |
64 def processEnded(self, reason): | |
65 info (u"process finished: %d" % (reason.value.exitCode,)) | |
66 self.parent.removeProcess(self.target, self) | |
67 | |
68 def write(self, message): | |
69 self.transport.write(message.encode('utf-8')) | |
70 | |
71 def boolOption(self, key): | |
72 """ Get boolean value from options | |
73 @param key: name of the option | |
74 @return: True if key exists and set to "true" (case insensitive), | |
75 False in all other cases """ | |
76 value = self.options.get(key, "") | |
77 return value.lower() == "true" | |
78 | |
79 | |
80 class CommandExport(object): | |
81 """Command export plugin: export a command to an entity""" | |
82 #XXX: This plugin can be potentially dangerous if we don't trust entities linked | |
83 # this is specially true if we have other triggers. | |
84 | |
85 def __init__(self, host): | |
86 info(_("Plugin command export initialization")) | |
87 self.host = host | |
88 self.spawned = {} # key = entity | |
89 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000) | |
90 host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand) | |
91 | |
92 def removeProcess(self, entity, process): | |
93 """ Called when the process is finished | |
94 @param entity: jid.JID attached to the process | |
95 @param process: process to remove""" | |
96 try: | |
97 processes_set = self.spawned[(entity, process.profile)] | |
98 processes_set.discard(process) | |
99 if not processes_set: | |
100 del(self.spawned[(entity, process.profile)]) | |
101 except ValueError: | |
102 pass | |
103 | |
663 | 104 def MessageReceivedTrigger(self, message, post_treat, profile): |
604 | 105 """ Check if source is linked and repeat message, else do nothing """ |
106 from_jid = jid.JID(message["from"]) | |
107 spawned_key = (from_jid.userhostJID(), profile) | |
108 try: | |
109 body = [e for e in message.elements() if e.name == 'body'][0] | |
110 except IndexError: | |
636
7ea6d5a86e58
plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
111 # do not block message without body (chat state notification...) |
7ea6d5a86e58
plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
112 warning("No body element found in message, following normal behaviour") |
7ea6d5a86e58
plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
113 return True |
604 | 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 |