From 28977af7769c2e96ee23fbb626b0840462437a3c Mon Sep 17 00:00:00 2001
From: David Stainton <dstainton415@gmail.com>
Date: Mon, 22 Jun 2015 20:40:55 -0700
Subject: [PATCH] Finish writing the invite implementation

---
 src/allmydata/scripts/magic_folder_cli.py   | 60 ++++++++++++++++++---
 src/allmydata/scripts/tahoe_ls.py           |  1 -
 src/allmydata/test/test_cli_magic_folder.py |  2 +-
 3 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/src/allmydata/scripts/magic_folder_cli.py b/src/allmydata/scripts/magic_folder_cli.py
index efc5e0b4..5a88ff75 100644
--- a/src/allmydata/scripts/magic_folder_cli.py
+++ b/src/allmydata/scripts/magic_folder_cli.py
@@ -1,12 +1,12 @@
 
-import os
+import os, json
 from cStringIO import StringIO
 from twisted.python import usage, failure
 
 from allmydata.scripts.common import BaseOptions
 from .common import BaseOptions, BasedirOptions, get_aliases
-from .cli import MakeDirectoryOptions
-
+from .cli import MakeDirectoryOptions, ListOptions, LnOptions
+import tahoe_ls, tahoe_mv
 
 class CreateOptions(BasedirOptions):
     nickname = None
@@ -40,21 +40,69 @@ class InviteOptions(BasedirOptions):
         aliases = get_aliases(self['node-directory'])
         self.aliases = aliases
 
+def diminish_readonly(write_cap, node_url):
+    """
+    given a write cap and a node url I will return the corresponding readcap
+    or I'll return None on failure
+    """
+    list_options = ListOptions()
+    list_options.where = u"%s" % (write_cap,)
+    list_options["json"] = True
+    list_options.aliases = {}
+    list_options.stdin = StringIO("")
+    list_options.stdout = StringIO()
+    list_options.stderr = StringIO()
+    list_options['node-url'] = node_url
+
+    rc = tahoe_ls.list(list_options)
+    if rc != 0:
+        return None
+
+    ls_json = list_options.stdout.getvalue()
+    readonly_cap = json.loads(ls_json)[1][u"ro_uri"]
+    return readonly_cap
+
 def invite(options):
     from allmydata.scripts import tahoe_mkdir
     mkdir_options = MakeDirectoryOptions()
-    mkdir_options.where = "%s:%s" % (options.alias, options.nickname)
+    mkdir_options.where = None
     mkdir_options.stdout = options.stdout
     mkdir_options.stdin = options.stdin
     mkdir_options.stderr = options.stderr
     mkdir_options['node-url'] = options['node-url']
     mkdir_options.aliases = options.aliases
     mkdir_options['node-directory'] = options['node-directory']
+
     rc = tahoe_mkdir.mkdir(mkdir_options)
     if rc != 0:
+        # XXX failure
+        print "tahoe mkdir FAIL"
         return rc
-    write_capability = mkdir_options.stdout.getvalue().strip()
-    print "\nNEW write cap %s" % (write_capability,)
+    dmd_write_cap = mkdir_options.stdout.getvalue().strip()
+    dmd_readonly_cap = diminish_readonly(dmd_write_cap, options["node-url"])
+    if dmd_readonly_cap is None:
+        # XXX failure
+        print "failure to diminish dmd write cap"
+        return -1
+
+    magic_write_cap = get_aliases(options["node-directory"])[options.alias]
+    magic_readonly_cap = diminish_readonly(magic_write_cap, options["node-url"])
+
+    # tahoe ln CLIENT_READCAP COLLECTIVE_WRITECAP/NICKNAME
+    ln_options = LnOptions()
+    ln_options["node-url"] = options["node-url"]
+    ln_options.from_file = dmd_readonly_cap
+    ln_options.to_file = "%s/%s" % (magic_write_cap, options.nickname)
+    ln_options.aliases = options.aliases
+    ln_options.stdin = StringIO("")
+    ln_options.stdout = StringIO()
+    ln_options.stderr = StringIO()
+    rc = tahoe_mv.mv(ln_options, mode="link")
+    if rc != 0:
+        # XXX failure
+        print "tahoe ln FAIL"
+        return -1
+    print "\ninvite code:\n%s-%s\n" % (magic_readonly_cap, dmd_write_cap)
     return rc
 
 class JoinOptions(BasedirOptions):
diff --git a/src/allmydata/scripts/tahoe_ls.py b/src/allmydata/scripts/tahoe_ls.py
index 78eea1f2..530cf850 100644
--- a/src/allmydata/scripts/tahoe_ls.py
+++ b/src/allmydata/scripts/tahoe_ls.py
@@ -151,7 +151,6 @@ def list(options):
             line.append(uri)
         if options["readonly-uri"]:
             line.append(quote_output(ro_uri or "-", quotemarks=False))
-
         rows.append((encoding_error, line))
 
     max_widths = []
diff --git a/src/allmydata/test/test_cli_magic_folder.py b/src/allmydata/test/test_cli_magic_folder.py
index 684e1c91..820ac446 100644
--- a/src/allmydata/test/test_cli_magic_folder.py
+++ b/src/allmydata/test/test_cli_magic_folder.py
@@ -25,7 +25,7 @@ class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase):
         return d
 
     def _invite(self, ignore):
-        d = self.do_cli("magic-folder", "invite", "magic", u"Alice:")
+        d = self.do_cli("magic-folder", "invite", u"magic", u"Alice")
         return d
 
     def test_create_invite_join(self):
-- 
2.45.2