- 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);
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- } catch (AssertionError e) {
- handleAssertionError(e);
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- } catch (AttachmentInvalidException e) {
- System.err.println("Failed to add attachment: " + e.getMessage());
- System.err.println("Aborting sending.");
- System.exit(1);
- } catch (DBusExecutionException e) {
- handleDBusExecutionException(e);
- } catch (UntrustedIdentityException e) {
- e.printStackTrace();
- }
- }
-
- 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: %d\nBody: %s\n",
- s.getSender(), 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();
- }
- while (true) {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- System.exit(0);
- }
- }
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(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());
- System.exit(3);
- } catch (AssertionError e) {
- handleAssertionError(e);
- }
- break;
- case "quitGroup":
- if (dBusConn != null) {
- System.err.println("quitGroup is not yet implemented via dbus");
- System.exit(1);
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(1);
- }
-
- try {
- m.sendQuitGroupMessage(decodeGroupId(ns.getString("group")));
- } catch (IOException e) {
- handleIOException(e);
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- } catch (AssertionError e) {
- handleAssertionError(e);
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- } catch (UntrustedIdentityException e) {
- e.printStackTrace();
- }
-
- break;
- case "updateGroup":
- if (dBusConn != null) {
- System.err.println("updateGroup is not yet implemented via dbus");
- System.exit(1);
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(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);
- } catch (AttachmentInvalidException e) {
- System.err.println("Failed to add avatar attachment for group\": " + e.getMessage());
- System.err.println("Aborting sending.");
- System.exit(1);
- } catch (GroupNotFoundException e) {
- handleGroupNotFoundException(e);
- } catch (EncapsulatedExceptions e) {
- handleEncapsulatedExceptions(e);
- } catch (UntrustedIdentityException e) {
- e.printStackTrace();
- }
-
- break;
- case "daemon":
- if (dBusConn != null) {
- System.err.println("Stop it.");
- System.exit(1);
- }
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(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 (DBusException e) {
- e.printStackTrace();
- System.exit(3);
- }
- try {
- m.receiveMessages(3600, false, new DbusReceiveMessageHandler(m, conn));
- } catch (IOException e) {
- System.err.println("Error while receiving messages: " + e.getMessage());
- System.exit(3);
- } catch (AssertionError e) {
- handleAssertionError(e);
- }
- } finally {
- if (conn != null) {
- conn.disconnect();
- }
- }
-
- break;
- }
- System.exit(0);
- } finally {
- if (dBusConn != null) {
- dBusConn.disconnect();