Implement daemon mode with dbus interface

This commit is contained in:
AsamK 2015-12-12 21:30:24 +01:00
parent 845e93ec0f
commit 9a1b348ed2
4 changed files with 74 additions and 9 deletions

View 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);

View 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;
}
}

View 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;
}