changeset 1670:3690b4d4157e

jp (pipe): pipe commands now use the new CommandAnswering API (with actionNew)
author Goffi <goffi@goffi.org>
date Wed, 25 Nov 2015 02:06:44 +0100
parents 697effba0310
children 1895846fc9cb
files frontends/src/jp/cmd_pipe.py
diffstat 1 files changed, 50 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pipe.py	Wed Nov 25 02:04:43 2015 +0100
+++ b/frontends/src/jp/cmd_pipe.py	Wed Nov 25 02:06:44 2015 +0100
@@ -25,23 +25,27 @@
 import os.path
 import shutil
 from sat.core.i18n import _
+from sat_frontends.tools import jid
+import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
+
 
 __commands__ = ["Pipe"]
 
+
 class PipeOut(base.CommandBase):
 
     def __init__(self, host):
-        super(PipeOut, self).__init__(host, 'out', help=_('Pipe a stream out'))
+        super(PipeOut, self).__init__(host, 'out', help=_('send a pipe a stream'))
 
     def add_parser_options(self):
-        self.parser.add_argument("jid", type=base.unicode_decoder, help=_("The destination jid"))
+        self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid"))
 
     def pipe_out(self):
         """ Create named pipe, and send stdin to it """
         tmp_dir = tempfile.mkdtemp()
         fifopath = os.path.join(tmp_dir,"pipe_out")
         os.mkfifo(fifopath)
-        self.host.bridge.pipeOut(self.host.get_full_jid(self.args.jid), fifopath, {}, self.profile)
+        self.host.bridge.pipeOut(self.host.get_full_jid(self.args.jid), fifopath, self.profile)
         with open(fifopath, 'w') as f:
             shutil.copyfileobj(sys.stdin, f)
         shutil.rmtree(tmp_dir)
@@ -55,33 +59,58 @@
 
 
 class PipeIn(base.CommandAnswering):
-    confirm_type = "PIPE_TRANSFER"
 
     def __init__(self, host):
-        super(PipeIn, self).__init__(host, 'in', help=_('Wait for the reception of a pipe stream'))
-
-    @property
-    def dest_jids(self):
-        return self.args.jids
+        super(PipeIn, self).__init__(host, 'in', help=_('receive a pipe stream'))
+        self.action_callbacks = {"PIPE": self.onPipeAction}
 
     def add_parser_options(self):
         self.parser.add_argument("jids", type=base.unicode_decoder, nargs="*", help=_('Jids accepted (none means "accept everything")'))
 
-    def ask(self, data, confirm_id):
-        answer_data = {}
-        tmp_dir = tempfile.mkdtemp()
-        fifopath = os.path.join(tmp_dir,"pipe_in")
-        answer_data["dest_path"] = fifopath
-        os.mkfifo(fifopath)
-        self.host.bridge.confirmationAnswer(confirm_id, True, answer_data, self.profile)
-        with open(fifopath, 'r') as f:
-            shutil.copyfileobj(f, sys.stdout)
-        shutil.rmtree(tmp_dir)
-        self.host.quit()
+    def getXmluiId(self, action_data):
+        # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
+        #        should be available in the futur
+        # TODO: XMLUI module
+        try:
+            xml_ui = action_data['xmlui']
+        except KeyError:
+            self.disp(_(u"Action has no XMLUI"), 1)
+        else:
+            ui = ET.fromstring(xml_ui.encode('utf-8'))
+            xmlui_id = ui.get('submit')
+            if not xmlui_id:
+                self.disp(_(u"Invalid XMLUI received"), error=True)
+            return xmlui_id
+
+    def onPipeAction(self, action_data, action_id, security_limit, profile):
+        xmlui_id = self.getXmluiId(action_data)
+        if xmlui_id is None:
+            return self.host.quitFromSignal(1)
+        try:
+            from_jid = jid.JID(action_data['meta_from_jid'])
+        except KeyError:
+            self.disp(_(u"Ignoring action without from_jid data"), 1)
+            return
+
+        if not self.bare_jids or from_jid.bare in self.bare_jids:
+            tmp_dir = tempfile.mkdtemp()
+            fifopath = os.path.join(tmp_dir,"pipe_in")
+            os.mkfifo(fifopath)
+            xmlui_data = {'path': fifopath}
+            self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
+
+            with open(fifopath, 'r') as f:
+                shutil.copyfileobj(f, sys.stdout)
+            shutil.rmtree(tmp_dir)
+            self.host.quit()
+
+    def run(self):
+        super(PipeIn, self).run()
+        self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
 
 
 class Pipe(base.CommandBase):
     subcommands = (PipeOut, PipeIn)
 
     def __init__(self, host):
-        super(Pipe, self).__init__(host, 'pipe', use_profile=False, help=_('Stream piping through XMPP'))
+        super(Pipe, self).__init__(host, 'pipe', use_profile=False, help=_('stream piping through XMPP'))