annotate src/plugins/plugin_exp_command_export.py @ 606:21ddafccf32d

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