- 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 (UnsatisfiedLinkError e) {
- System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
- return 1;
- } 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;
- }
- double timeout = 5;
- if (ns.getDouble("timeout") != null) {
- timeout = ns.getDouble("timeout");
- }
- boolean returnOnTimeout = true;
- if (timeout < 0) {
- returnOnTimeout = false;
- timeout = 3600;
- }
- boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
- try {
- m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, returnOnTimeout, ignoreAttachments, 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;
- }
-
- break;
- case "updateGroup":
- if (dBusConn != null) {
- System.err.println("updateGroup is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
-
- try {
- byte[] groupId = null;
- if (ns.getString("group") != null) {
- groupId = decodeGroupId(ns.getString("group"));
- }
- byte[] newGroupId = m.sendUpdateGroupMessage(groupId, ns.getString("name"), ns.<String>getList("member"), ns.getString("avatar"));
- if (groupId == null) {
- System.out.println("Creating new group \"" + Base64.encodeBytes(newGroupId) + "\" …");
- }
- } catch (IOException e) {
- handleIOException(e);
- return 3;
- } catch (AttachmentInvalidException e) {
- System.err.println("Failed to add avatar attachment for group\": " + e.getMessage());
- System.err.println("Aborting sending.");
- return 1;
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- return 1;
- } catch (NotAGroupMemberException e) {
- handleNotAGroupMemberException(e);
- return 1;
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- return 3;
- }
-
- break;
- case "listIdentities":
- if (dBusConn != null) {
- System.err.println("listIdentities is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
- if (ns.get("number") == null) {
- for (Map.Entry<String, List<JsonIdentityKeyStore.Identity>> keys : m.getIdentities().entrySet()) {
- for (JsonIdentityKeyStore.Identity id : keys.getValue()) {
- printIdentityFingerprint(m, keys.getKey(), id);
- }
- }
- } else {
- String number = ns.getString("number");
- for (JsonIdentityKeyStore.Identity id : m.getIdentities(number)) {
- printIdentityFingerprint(m, number, id);
- }
- }
- break;
- case "trust":
- if (dBusConn != null) {
- System.err.println("trust is not yet implemented via dbus");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
- String number = ns.getString("number");
- if (ns.getBoolean("trust_all_known_keys")) {
- boolean res = m.trustIdentityAllKeys(number);
- if (!res) {
- System.err.println("Failed to set the trust for this number, make sure the number is correct.");
- return 1;
- }
- } else {
- String fingerprint = ns.getString("verified_fingerprint");
- if (fingerprint != null) {
- fingerprint = fingerprint.replaceAll(" ", "");
- if (fingerprint.length() == 66) {
- byte[] fingerprintBytes;
- try {
- fingerprintBytes = Hex.toByteArray(fingerprint.toLowerCase(Locale.ROOT));
- } catch (Exception e) {
- System.err.println("Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
- return 1;
- }
- boolean res = m.trustIdentityVerified(number, fingerprintBytes);
- if (!res) {
- System.err.println("Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
- return 1;
- }
- } else if (fingerprint.length() == 60) {
- boolean res = m.trustIdentityVerifiedSafetyNumber(number, fingerprint);
- if (!res) {
- System.err.println("Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
- return 1;
- }
- } else {
- System.err.println("Fingerprint has invalid format, either specify the old hex fingerprint or the new safety number");
- return 1;
- }
- } else {
- System.err.println("You need to specify the fingerprint you have verified with -v FINGERPRINT");
- return 1;
- }
- }
- break;
- case "daemon":
- if (dBusConn != null) {
- System.err.println("Stop it.");
- return 1;
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- return 1;
- }
- DBusConnection conn = null;
- try {
- try {
- int busType;
- if (ns.getBoolean("system")) {
- busType = DBusConnection.SYSTEM;
- } else {
- busType = DBusConnection.SESSION;
- }
- conn = DBusConnection.getConnection(busType);
- conn.exportObject(SIGNAL_OBJECTPATH, m);
- conn.requestBusName(SIGNAL_BUSNAME);
- } catch (UnsatisfiedLinkError e) {
- System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
- return 1;
- } catch (DBusException e) {
- e.printStackTrace();
- return 2;
- }
- ignoreAttachments = ns.getBoolean("ignore_attachments");
- try {
- m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, new DbusReceiveMessageHandler(m, conn));
- } catch (IOException e) {
- System.err.println("Error while receiving messages: " + e.getMessage());
- return 3;
- } catch (AssertionError e) {
- handleAssertionError(e);
- return 1;
- }
- } finally {
- if (conn != null) {
- conn.disconnect();
- }
- }
-
- break;
- }
- return 0;
- } finally {
- if (dBusConn != null) {
- dBusConn.disconnect();