mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Implement daemon mode with dbus interface
This commit is contained in:
parent
845e93ec0f
commit
9a1b348ed2
4 changed files with 74 additions and 9 deletions
|
@ -9,6 +9,9 @@ mainClassName = 'cli.Main'
|
||||||
version = '0.1.0'
|
version = '0.1.0'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
maven {
|
||||||
|
url "https://raw.github.com/AsamK/maven/master/releases/"
|
||||||
|
}
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +20,7 @@ dependencies {
|
||||||
compile 'com.madgag.spongycastle:prov:1.53.0.0'
|
compile 'com.madgag.spongycastle:prov:1.53.0.0'
|
||||||
compile 'commons-io:commons-io:2.4'
|
compile 'commons-io:commons-io:2.4'
|
||||||
compile 'net.sourceforge.argparse4j:argparse4j:0.5.0'
|
compile 'net.sourceforge.argparse4j:argparse4j:0.5.0'
|
||||||
|
compile 'org.freedesktop.dbus:dbus-java:2.7.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
jar {
|
jar {
|
||||||
|
|
|
@ -20,6 +20,8 @@ import net.sourceforge.argparse4j.ArgumentParsers;
|
||||||
import net.sourceforge.argparse4j.impl.Arguments;
|
import net.sourceforge.argparse4j.impl.Arguments;
|
||||||
import net.sourceforge.argparse4j.inf.*;
|
import net.sourceforge.argparse4j.inf.*;
|
||||||
import org.apache.commons.io.IOUtils;
|
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.crypto.UntrustedIdentityException;
|
||||||
import org.whispersystems.textsecure.api.messages.*;
|
import org.whispersystems.textsecure.api.messages.*;
|
||||||
import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
|
import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
|
||||||
|
@ -155,13 +157,10 @@ public class Main {
|
||||||
try {
|
try {
|
||||||
m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
|
m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error while receiving message: " + e.getMessage());
|
System.err.println("Error while receiving messages: " + e.getMessage());
|
||||||
System.exit(3);
|
System.exit(3);
|
||||||
} catch (AssertionError e) {
|
} catch (AssertionError e) {
|
||||||
System.err.println("Failed to receive message (Assertion): " + e.getMessage());
|
handleAssertionError(e);
|
||||||
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 "quitGroup":
|
case "quitGroup":
|
||||||
|
@ -210,6 +209,35 @@ public class Main {
|
||||||
handleEncapsulatedExceptions(e);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
m.save();
|
m.save();
|
||||||
|
@ -296,6 +324,11 @@ public class Main {
|
||||||
.type(int.class)
|
.type(int.class)
|
||||||
.help("Number of seconds to wait for new messages (negative values disable timeout)");
|
.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 {
|
try {
|
||||||
Namespace ns = parser.parseArgs(args);
|
Namespace ns = parser.parseArgs(args);
|
||||||
if (ns.getString("username") == null) {
|
if (ns.getString("username") == null) {
|
||||||
|
@ -319,7 +352,7 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleAssertionError(AssertionError e) {
|
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(e.getStackTrace());
|
||||||
System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
|
System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
|
|
@ -50,7 +50,7 @@ import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
class Manager {
|
class Manager implements TextSecure {
|
||||||
private final static String URL = "https://textsecure-service.whispersystems.org";
|
private final static String URL = "https://textsecure-service.whispersystems.org";
|
||||||
private final static TrustStore TRUST_STORE = new WhisperTrustStore();
|
private final static TrustStore TRUST_STORE = new WhisperTrustStore();
|
||||||
|
|
||||||
|
@ -270,6 +270,7 @@ class Manager {
|
||||||
return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
|
return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void sendGroupMessage(String messageText, List<String> attachments,
|
public void sendGroupMessage(String messageText, List<String> attachments,
|
||||||
byte[] groupId)
|
byte[] groupId)
|
||||||
throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
|
throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
|
||||||
|
@ -351,9 +352,17 @@ class Manager {
|
||||||
return g.groupId;
|
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,
|
public void sendMessage(String messageText, List<String> attachments,
|
||||||
Collection<String> recipients)
|
Collection<String> recipients)
|
||||||
throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
|
throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
|
||||||
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
|
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
|
||||||
if (attachments != null) {
|
if (attachments != null) {
|
||||||
messageBuilder.withAttachments(getTextSecureAttachments(attachments));
|
messageBuilder.withAttachments(getTextSecureAttachments(attachments));
|
||||||
|
@ -394,6 +403,7 @@ class Manager {
|
||||||
handleEndSession(recipient.getNumber());
|
handleEndSession(recipient.getNumber());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
private TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
|
private TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
|
||||||
|
@ -498,6 +508,7 @@ class Manager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
save();
|
||||||
handler.handleMessage(envelope, content, group);
|
handler.handleMessage(envelope, content, group);
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
if (returnOnTimeout)
|
if (returnOnTimeout)
|
||||||
|
@ -505,7 +516,6 @@ class Manager {
|
||||||
} catch (InvalidVersionException e) {
|
} catch (InvalidVersionException e) {
|
||||||
System.err.println("Ignoring error: " + e.getMessage());
|
System.err.println("Ignoring error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
save();
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (messagePipe != null)
|
if (messagePipe != null)
|
||||||
|
@ -581,4 +591,9 @@ class Manager {
|
||||||
private void setGroupInfo(GroupInfo group) {
|
private void setGroupInfo(GroupInfo group) {
|
||||||
groupStore.updateGroup(group);
|
groupStore.updateGroup(group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRemote() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
13
src/main/java/cli/TextSecure.java
Normal file
13
src/main/java/cli/TextSecure.java
Normal file
|
@ -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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue