- switch (ns.getString("command")) {
- case "register":
- if (dBusConn != null) {
- System.err.println("register is not yet implemented via dbus");
- return 1;
- }
- if (!m.userHasKeys()) {
- m.createNewIdentity();
- }
- try {
- m.register(ns.getBoolean("voice"));
- } catch (IOException e) {
- System.err.println("Request verify error: " + e.getMessage());
- return 3;
- }
- break;
- case "verify":
- if (dBusConn != null) {
- System.err.println("verify is not yet implemented via dbus");
- return 1;
- }
- if (!m.userHasKeys()) {
- System.err.println("User has no keys, first call register.");
- return 1;
- }
- if (m.isRegistered()) {
- System.err.println("User registration is already verified");
- return 1;
- }
- try {
- m.verifyAccount(ns.getString("verificationCode"));
- } catch (IOException e) {
- System.err.println("Verify error: " + e.getMessage());
- return 3;
- }
- break;
- case "link":
- if (dBusConn != null) {
- System.err.println("link is not yet implemented via dbus");
- return 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.");
- return 3;
- } catch (IOException e) {
- System.err.println("Link request error: " + e.getMessage());
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- return 2;
- } catch (UserAlreadyExists e) {
- System.err.println("The user " + e.getUsername() + " already exists\nDelete \"" + e.getFileName() + "\" before trying again.");
- return 1;
- }
- break;
- case "addDevice":
- if (dBusConn != null) {
- System.err.println("link is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
- try {
- m.addDeviceLink(new URI(ns.getString("uri")));
- } catch (IOException e) {
- e.printStackTrace();
- return 3;
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- return 2;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- } catch (URISyntaxException e) {
- e.printStackTrace();
- return 2;
- }
- break;
- case "listDevices":
- if (dBusConn != null) {
- System.err.println("listDevices is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 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: " + formatTimestamp(d.getCreated()));
- System.out.println(" Last seen: " + formatTimestamp(d.getLastSeen()));
- }
- } catch (IOException e) {
- e.printStackTrace();
- return 3;
- }
- break;
- case "removeDevice":
- if (dBusConn != null) {
- System.err.println("removeDevice is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
- try {
- int deviceId = ns.getInt("deviceId");
- m.removeLinkedDevices(deviceId);
- } catch (IOException e) {
- e.printStackTrace();
- return 3;
- }
- break;
- case "send":
- if (dBusConn == null && !m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
-
- if (ns.getBoolean("endsession")) {
- if (ns.getList("recipient") == null) {
- System.err.println("No recipients given");
- System.err.println("Aborting sending.");
- return 1;
- }
- try {
- ts.sendEndSessionMessage(ns.<String>getList("recipient"));
- } catch (IOException e) {
- handleIOException(e);
- return 3;
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- } catch (DBusExecutionException e) {
- handleDBusExecutionException(e);
- return 1;
- }
- } else {
- String messageText = ns.getString("message");
- if (messageText == null) {
- try {
- messageText = readAll(System.in);
- } catch (IOException e) {
- System.err.println("Failed to read message from stdin: " + e.getMessage());
- System.err.println("Aborting sending.");
- return 1;
- }
- }
-
- try {
- List<String> attachments = ns.getList("attachment");
- if (attachments == null) {
- attachments = new ArrayList<>();
- }
- if (ns.getString("group") != null) {
- byte[] groupId = decodeGroupId(ns.getString("group"));
- ts.sendGroupMessage(messageText, attachments, groupId);
- } else {
- ts.sendMessage(messageText, attachments, ns.<String>getList("recipient"));
- }
- } catch (IOException e) {
- handleIOException(e);
- return 3;
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- return 1;
- } catch (NotAGroupMemberException e) {
- handleNotAGroupMemberException(e);
- return 1;
- } catch (AttachmentInvalidException e) {
- System.err.println("Failed to add attachment: " + e.getMessage());
- System.err.println("Aborting sending.");
- return 1;
- } catch (DBusExecutionException e) {
- handleDBusExecutionException(e);
- return 1;
- }
- }
-
- break;
- case "receive":
- if (dBusConn != null) {
- try {
- dBusConn.addSigHandler(Signal.MessageReceived.class, new DBusSigHandler<Signal.MessageReceived>() {
- @Override
- public void handle(Signal.MessageReceived s) {
- System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
- s.getSender(), formatTimestamp(s.getTimestamp()), s.getMessage()));
- if (s.getGroupId().length > 0) {
- System.out.println("Group info:");
- System.out.println(" Id: " + Base64.encodeBytes(s.getGroupId()));
- }
- if (s.getAttachments().size() > 0) {
- System.out.println("Attachments: ");
- for (String attachment : s.getAttachments()) {
- System.out.println("- Stored plaintext in: " + attachment);
- }
- }
- System.out.println();
- }
- });
- } catch (DBusException e) {
- e.printStackTrace();
- return 1;
- }
- while (true) {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- return 0;
- }
- }
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
- int timeout = 5;
- if (ns.getInt("timeout") != null) {
- timeout = ns.getInt("timeout");
- }
- boolean returnOnTimeout = true;
- if (timeout < 0) {
- returnOnTimeout = false;
- timeout = 3600;
- }
- try {
- m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
- } catch (IOException e) {
- System.err.println("Error while receiving messages: " + e.getMessage());
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- }
- break;
- case "quitGroup":
- if (dBusConn != null) {
- System.err.println("quitGroup is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
-
- try {
- m.sendQuitGroupMessage(decodeGroupId(ns.getString("group")));
- } catch (IOException e) {
- handleIOException(e);
- return 3;
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- return 1;
- } catch (NotAGroupMemberException e) {
- handleNotAGroupMemberException(e);
- return 1;
- }