]> nmode's Git Repositories - signal-cli/commitdiff
Implement daemon mode with dbus interface
authorAsamK <asamk@gmx.de>
Sat, 12 Dec 2015 20:30:24 +0000 (21:30 +0100)
committerAsamK <asamk@gmx.de>
Sat, 12 Dec 2015 20:30:24 +0000 (21:30 +0100)
build.gradle
src/main/java/cli/Main.java
src/main/java/cli/Manager.java
src/main/java/cli/TextSecure.java [new file with mode: 0644]

index c56e0d85da8e2a4b321d97cd3d8ab1d7758d00dd..1989339139d510cc693b3cbee129ecc15b5431ee 100644 (file)
@@ -9,6 +9,9 @@ mainClassName = 'cli.Main'
 version = '0.1.0'
 
 repositories {
+    maven {
+        url "https://raw.github.com/AsamK/maven/master/releases/"
+    }
     mavenCentral()
 }
 
@@ -17,6 +20,7 @@ dependencies {
     compile 'com.madgag.spongycastle:prov:1.53.0.0'
     compile 'commons-io:commons-io:2.4'
     compile 'net.sourceforge.argparse4j:argparse4j:0.5.0'
+    compile 'org.freedesktop.dbus:dbus-java:2.7.0'
 }
 
 jar {
index ae2f23fa46b57d28ff2e4c2562f26f3ce7d5ba29..64bdfae3be86fa9ca87ab15abb0cb70e6ad9072d 100644 (file)
@@ -20,6 +20,8 @@ import net.sourceforge.argparse4j.ArgumentParsers;
 import net.sourceforge.argparse4j.impl.Arguments;
 import net.sourceforge.argparse4j.inf.*;
 import org.apache.commons.io.IOUtils;
+import org.freedesktop.dbus.DBusConnection;
+import org.freedesktop.dbus.exceptions.DBusException;
 import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
 import org.whispersystems.textsecure.api.messages.*;
 import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
@@ -155,13 +157,10 @@ public class Main {
                 try {
                     m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
                 } catch (IOException e) {
-                    System.err.println("Error while receiving message: " + e.getMessage());
+                    System.err.println("Error while receiving messages: " + 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);
+                    handleAssertionError(e);
                 }
                 break;
             case "quitGroup":
@@ -210,6 +209,35 @@ public class Main {
                     handleEncapsulatedExceptions(e);
                 }
 
+                break;
+            case "daemon":
+                if (!m.isRegistered()) {
+                    System.err.println("User is not registered.");
+                    System.exit(1);
+                }
+                try {
+                    int busType;
+                    if (ns.getBoolean("system")) {
+                        busType = DBusConnection.SYSTEM;
+                    } else {
+                        busType = DBusConnection.SESSION;
+                    }
+                    DBusConnection 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);
+                }
+
                 break;
         }
         m.save();
@@ -296,6 +324,11 @@ public class Main {
                 .type(int.class)
                 .help("Number of seconds to wait for new messages (negative values disable timeout)");
 
+        Subparser parserDaemon = subparsers.addParser("daemon");
+        parserDaemon.addArgument("--system")
+                .action(Arguments.storeTrue())
+                .help("Use DBus system bus instead of user bus.");
+
         try {
             Namespace ns = parser.parseArgs(args);
             if (ns.getString("username") == null) {
@@ -319,7 +352,7 @@ public class Main {
     }
 
     private static void handleAssertionError(AssertionError e) {
-        System.err.println("Failed to send message (Assertion): " + e.getMessage());
+        System.err.println("Failed to send/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);
index 2a1ddb220de886683dcae85b7267a33169b08777..b15457af5b5fde47b1fa0a837ab743338e0a6513 100644 (file)
@@ -50,7 +50,7 @@ import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
-class Manager {
+class Manager implements TextSecure {
     private final static String URL = "https://textsecure-service.whispersystems.org";
     private final static TrustStore TRUST_STORE = new WhisperTrustStore();
 
@@ -270,6 +270,7 @@ class Manager {
         return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
     }
 
+    @Override
     public void sendGroupMessage(String messageText, List<String> attachments,
                                  byte[] groupId)
             throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
@@ -351,9 +352,17 @@ class Manager {
         return g.groupId;
     }
 
+    @Override
+    public void sendMessage(String message, List<String> attachments, String recipient)
+            throws EncapsulatedExceptions, AttachmentInvalidException, IOException {
+        List<String> recipients = new ArrayList<>(1);
+        recipients.add(recipient);
+        sendMessage(message, attachments, recipients);
+    }
+
     public void sendMessage(String messageText, List<String> attachments,
                             Collection<String> recipients)
-            throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
+            throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
         final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
         if (attachments != null) {
             messageBuilder.withAttachments(getTextSecureAttachments(attachments));
@@ -394,6 +403,7 @@ class Manager {
                 handleEndSession(recipient.getNumber());
             }
         }
+        save();
     }
 
     private TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
@@ -498,6 +508,7 @@ class Manager {
                             }
                         }
                     }
+                    save();
                     handler.handleMessage(envelope, content, group);
                 } catch (TimeoutException e) {
                     if (returnOnTimeout)
@@ -505,7 +516,6 @@ class Manager {
                 } catch (InvalidVersionException e) {
                     System.err.println("Ignoring error: " + e.getMessage());
                 }
-                save();
             }
         } finally {
             if (messagePipe != null)
@@ -581,4 +591,9 @@ class Manager {
     private void setGroupInfo(GroupInfo group) {
         groupStore.updateGroup(group);
     }
+
+    @Override
+    public boolean isRemote() {
+        return false;
+    }
 }
diff --git a/src/main/java/cli/TextSecure.java b/src/main/java/cli/TextSecure.java
new file mode 100644 (file)
index 0000000..011e696
--- /dev/null
@@ -0,0 +1,13 @@
+package cli;
+
+import org.freedesktop.dbus.DBusInterface;
+import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
+
+import java.io.IOException;
+import java.util.List;
+
+public interface TextSecure extends DBusInterface {
+    void sendMessage(String message, List<String> attachments, String recipient) throws EncapsulatedExceptions, AttachmentInvalidException, IOException;
+
+    void sendGroupMessage(String message, List<String> attachments, byte[] groupId) throws EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, IOException;
+}