+ case "link":
+ if (dBusConn != null) {
+ System.err.println("link is not yet implemented via dbus");
+ System.exit(1);
+ }
+
+ // When linking, username is null and we always have to create keys
+ m.createNewIdentity();
+
+ String deviceName = ns.getString("name");
+ if (deviceName == null) {
+ deviceName = "cli";
+ }
+ try {
+ System.out.println(m.getDeviceLinkUri());
+ m.finishDeviceLink(deviceName);
+ System.out.println("Associated with: " + m.getUsername());
+ } catch (TimeoutException e) {
+ System.err.println("Link request timed out, please try again.");
+ System.exit(3);
+ } catch (IOException e) {
+ System.err.println("Link request error: " + e.getMessage());
+ System.exit(3);
+ } catch (InvalidKeyException e) {
+ e.printStackTrace();
+ System.exit(3);
+ } catch (UserAlreadyExists e) {
+ System.err.println("The user " + e.getUsername() + " already exists\nDelete \"" + e.getFileName() + "\" before trying again.");
+ System.exit(3);
+ }
+ break;
+ case "addDevice":
+ if (dBusConn != null) {
+ System.err.println("link is not yet implemented via dbus");
+ System.exit(1);
+ }
+ if (!m.isRegistered()) {
+ System.err.println("User is not registered.");
+ System.exit(1);
+ }
+ try {
+ m.addDeviceLink(new URI(ns.getString("uri")));
+ } catch (IOException e) {
+ e.printStackTrace();
+ System.exit(3);
+ } catch (InvalidKeyException e) {
+ e.printStackTrace();
+ System.exit(2);
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ System.exit(2);
+ }
+ break;
+ case "listDevices":
+ if (dBusConn != null) {
+ System.err.println("listDevices is not yet implemented via dbus");
+ System.exit(1);
+ }
+ if (!m.isRegistered()) {
+ System.err.println("User is not registered.");
+ System.exit(1);
+ }
+ try {
+ List<DeviceInfo> devices = m.getLinkedDevices();
+ for (DeviceInfo d : devices) {
+ System.out.println("Device " + d.getId() + (d.getId() == m.getDeviceId() ? " (this device)" : "") + ":");
+ System.out.println(" Name: " + d.getName());
+ System.out.println(" Created: " + d.getCreated());
+ System.out.println(" Last seen: " + d.getLastSeen());
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ System.exit(3);
+ }
+ break;
+ case "removeDevice":
+ if (dBusConn != null) {
+ System.err.println("removeDevice is not yet implemented via dbus");
+ System.exit(1);
+ }
+ if (!m.isRegistered()) {
+ System.err.println("User is not registered.");
+ System.exit(1);
+ }
+ try {
+ int deviceId = ns.getInt("deviceId");
+ m.removeLinkedDevices(deviceId);
+ } catch (IOException e) {
+ e.printStackTrace();
+ System.exit(3);
+ }
+ break;