Mercurial > libervia-backend
annotate src/plugins/plugin_exp_command_export.py @ 1963:a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
/!\ several features are temporarily disabled, like notifications in frontends
next step in refactoring, with the following changes:
- jp: updated jp message to follow changes in backend/bridge
- jp: added --lang, --subject, --subject_lang, and --type options to jp message + fixed unicode handling for jid
- quick_frontend (QuickApp, QuickChat):
- follow backend changes
- refactored chat, message are now handled in OrderedDict and uid are kept so they can be updated
- Message and Occupant classes handle metadata, so frontend just have to display them
- Primitivus (Chat):
- follow backend/QuickFrontend changes
- info & standard messages are handled in the same MessageWidget class
- improved/simplified handling of messages, removed update() method
- user joined/left messages are merged when next to each other
- a separator is shown when message is received while widget is out of focus, so user can quickly see the new messages
- affiliation/role are shown (in a basic way for now) in occupants panel
- removed "/me" messages handling, as it will be done by a backend plugin
- message language is displayed when available (only one language per message for now)
- fixed :history and :search commands
- core (constants): new constants for messages type, XML namespace, entity type
- core: *Message methods renamed to follow new code sytle (e.g. sendMessageToBridge => messageSendToBridge)
- core (messages handling): fixed handling of language
- core (messages handling): mes_data['from'] and ['to'] are now jid.JID
- core (core.xmpp): reorganised message methods, added getNick() method to client.roster
- plugin text commands: fixed plugin and adapted to new messages behaviour. client is now used in arguments instead of profile
- plugins: added information for cancellation reason in CancelError calls
- plugin XEP-0045: various improvments, but this plugin still need work:
- trigger is used to avoid message already handled by the plugin to be handled a second time
- changed the way to handle history, the last message from DB is checked and we request only messages since this one, in seconds (thanks Poezio folks :))
- subject reception is waited before sending the roomJoined signal, this way we are sure that everything including history is ready
- cmd_* method now follow the new convention with client instead of profile
- roomUserJoined and roomUserLeft messages are removed, the events are now handled with info message with a "ROOM_USER_JOINED" info subtype
- probably other forgotten stuffs :p
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 20 Jun 2016 18:41:53 +0200 |
parents | 633b5c21aefd |
children | 1d3f73e065e1 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
604 | 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) |
1766 | 5 # Copyright (C) 2009-2016 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 _ |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
21 from sat.core.constants import Const as C |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
22 from sat.core.log import getLogger |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
23 log = getLogger(__name__) |
604 | 24 from twisted.words.protocols.jabber import jid |
25 from twisted.internet import reactor, protocol | |
26 | |
1374
0befb14ecf62
renamed tools.misc to tools.trigger
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
27 from sat.tools import trigger |
604 | 28 from sat.tools.utils import clean_ustr |
29 | |
30 PLUGIN_INFO = { | |
31 "name": "Command export plugin", | |
32 "import_name": "EXP-COMMANS-EXPORT", | |
33 "type": "EXP", | |
34 "protocols": [], | |
35 "dependencies": [], | |
36 "main": "CommandExport", | |
37 "handler": "no", | |
38 "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, target, options, profile): | |
45 self.parent = parent | |
46 self.target = target | |
47 self.options = options | |
48 self.profile = profile | |
49 | |
50 def _clean(self, data): | |
51 if not data: | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
52 log.error ("data should not be empty !") |
604 | 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): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
58 log.info("connectionMade :)") |
604 | 59 |
60 def outReceived(self, data): | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
61 self.parent.host.messageSend(self.target, {'': self._clean(data)}, no_trigger=True, profile_key=self.profile) |
604 | 62 |
63 def errReceived(self, data): | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
64 self.parent.host.messageSend(self.target, {'': self._clean(data)}, no_trigger=True, profile_key=self.profile) |
604 | 65 |
66 def processEnded(self, reason): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
67 log.info (u"process finished: %d" % (reason.value.exitCode,)) |
604 | 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 | |
87 def __init__(self, host): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
88 log.info(_("Plugin command export initialization")) |
604 | 89 self.host = host |
90 self.spawned = {} # key = entity | |
91 host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=10000) | |
92 host.bridge.addMethod("exportCommand", ".plugin", in_sign='sasasa{ss}s', out_sign='', method=self._exportCommand) | |
93 | |
94 def removeProcess(self, entity, process): | |
95 """ Called when the process is finished | |
96 @param entity: jid.JID attached to the process | |
97 @param process: process to remove""" | |
98 try: | |
99 processes_set = self.spawned[(entity, process.profile)] | |
100 processes_set.discard(process) | |
101 if not processes_set: | |
102 del(self.spawned[(entity, process.profile)]) | |
103 except ValueError: | |
104 pass | |
105 | |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
106 def MessageReceivedTrigger(self, client, message_elt, post_treat): |
604 | 107 """ Check if source is linked and repeat message, else do nothing """ |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
108 from_jid = jid.JID(message_elt["from"]) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
109 spawned_key = (from_jid.userhostJID(), client.profile) |
604 | 110 |
111 if spawned_key in self.spawned: | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
112 try: |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
113 body = message_elt.elements(C.NS_CLIENT, 'body').next() |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
114 except StopIteration: |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
115 # do not block message without body (chat state notification...) |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
116 return True |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
117 |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
118 mess_data = unicode(body) + '\n' |
604 | 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: | |
1374
0befb14ecf62
renamed tools.misc to tools.trigger
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
127 raise trigger.SkipOtherTriggers |
604 | 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: | |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
145 log.warning(u"Unknown profile [%s]" % (profile,)) |
604 | 146 return |
1374
0befb14ecf62
renamed tools.misc to tools.trigger
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
147 |
604 | 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() | |
1742
244a605623d6
complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents:
1654
diff
changeset
|
154 except (RuntimeError, jid.InvalidFormat, AttributeError): |
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 |