- }
- sendMessage(m, messageText, textSecureAttachments, recipients);
- break;
- case "receive":
- if (!m.isRegistered()) {
- System.err.println("User is not registered.");
- System.exit(1);
- }
- int timeout = ns.getInt("timeout");
- boolean returnOnTimeout = true;
- if (timeout < 0) {
- returnOnTimeout = false;
- timeout = 5;
- }
- try {
- m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
- } catch (IOException e) {
- System.err.println("Error while receiving message: " + e.getMessage());
- System.exit(3);
- } catch (AssertionError e) {
- System.err.println("Failed to receive message (Assertion): " + e.getMessage());
- System.err.println(e.getStackTrace());
- System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
- System.exit(1);
- }
- break;
+
+ 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.requestBusName("org.asamk.TextSecure");
+ conn.exportObject("/org/asamk/TextSecure", m);
+ } catch (DBusException e) {
+ e.printStackTrace();
+ System.exit(3);
+ }
+ try {
+ m.receiveMessages(3600, false, new ReceiveMessageHandler(m));
+ } 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();
+ }
+ }
+ }
+
+ private static void handleGroupNotFoundException(GroupNotFoundException e) {
+ System.err.println("Failed to send to group \"" + Base64.encodeBytes(e.getGroupId()) + "\": Unknown group");
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ }
+
+ private static byte[] decodeGroupId(String groupId) {
+ try {
+ return Base64.decode(groupId);
+ } catch (IOException e) {
+ System.err.println("Failed to decode groupId (must be base64) \"" + groupId + "\": " + e.getMessage());
+ System.err.println("Aborting sending.");
+ System.exit(1);
+ return null;