Mercurial > libervia-backend
annotate src/plugins/plugin_exp_command_export.py @ 1055:abcac1ac27a7
plugin otr: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 07 Jun 2014 16:39:08 +0200 |
parents | 301b342c697a |
children | 0befb14ecf62 |
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 _ |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
21 from sat.core.log import getLogger |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
22 log = getLogger(__name__) |
604 | 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: | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
51 log.error ("data should not be empty !") |
604 | 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): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
57 log.info("connectionMade :)") |
604 | 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): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
66 log.info (u"process finished: %d" % (reason.value.exitCode,)) |
604 | 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): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
87 log.info(_("Plugin command export initialization")) |
604 | 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 | |
663 | 105 def MessageReceivedTrigger(self, message, post_treat, profile): |
604 | 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: | |
636
7ea6d5a86e58
plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
112 # do not block message without body (chat state notification...) |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
113 log.warning("No body element found in message, following normal behaviour") |
636
7ea6d5a86e58
plugin XEP-0085: Chat State Notifications
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
114 return True |
604 | 115 |
116 mess_data = unicode(body) + '\n' | |
117 | |
118 if spawned_key in self.spawned: | |
119 processes_set = self.spawned[spawned_key] | |
120 _continue = False | |
121 exclusive = False | |
122 for process in processes_set: | |
123 process.write(mess_data) | |
124 _continue &= process.boolOption("continue") | |
125 exclusive |= process.boolOption("exclusive") | |
126 if exclusive: | |
127 raise SkipOtherTriggers | |
128 return _continue | |
129 | |
130 return True | |
131 | |
132 def _exportCommand(self, command, args, targets, options, profile_key): | |
133 """ Export a commands to authorised targets | |
134 @param command: full path of the command to execute | |
135 @param args: list of arguments, with command name as first one | |
136 @param targets: list of allowed entities | |
137 @param options: export options, a dict which can have the following keys ("true" to set booleans): | |
138 - exclusive: if set, skip all other triggers | |
139 - loop: if set, restart the command once terminated #TODO | |
140 - pty: if set, launch in a pseudo terminal | |
141 - continue: continue normal MessageReceived handling | |
142 """ | |
143 profile = self.host.memory.getProfileName(profile_key) | |
144 if not profile: | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
145 log.warning("Unknown profile [%s]" % (profile,)) |
604 | 146 return |
147 | |
148 for target in targets: | |
149 try: | |
150 _jid = jid.JID(target) | |
151 if not _jid.user or not _jid.host: | |
152 raise jid.InvalidFormat | |
153 _jid = _jid.userhostJID() | |
154 except (jid.InvalidFormat, RuntimeError): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
155 log.info(u"invalid target ignored: %s" % (target,)) |
604 | 156 continue |
157 process_prot = ExportCommandProtocol(self, _jid, options, profile) | |
158 self.spawned.setdefault((_jid, profile),set()).add(process_prot) | |
159 reactor.spawnProcess(process_prot, command, args, usePTY = process_prot.boolOption('pty')) | |
160 |