X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/d89e93ad473cc1c6dd5f4dc615b7d0c5721e3dc2..0a68303ca448f69a5655beb23f4213187dbce0b4:/src/main/java/org/asamk/signal/Manager.java diff --git a/src/main/java/org/asamk/signal/Manager.java b/src/main/java/org/asamk/signal/Manager.java index e5214302..d51e8b57 100644 --- a/src/main/java/org/asamk/signal/Manager.java +++ b/src/main/java/org/asamk/signal/Manager.java @@ -53,6 +53,7 @@ import org.whispersystems.signalservice.api.push.exceptions.*; import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; import org.whispersystems.signalservice.internal.push.SignalServiceProtos; +import org.whispersystems.signalservice.internal.push.SignalServiceUrl; import java.io.*; import java.net.URI; @@ -77,6 +78,7 @@ import static java.nio.file.attribute.PosixFilePermission.*; class Manager implements Signal { private final static String URL = "https://textsecure-service.whispersystems.org"; private final static TrustStore TRUST_STORE = new WhisperTrustStore(); + private final static SignalServiceUrl[] serviceUrls = new SignalServiceUrl[]{new SignalServiceUrl(URL, TRUST_STORE)}; public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle(); public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion(); @@ -108,6 +110,7 @@ class Manager implements Signal { private JsonGroupStore groupStore; private JsonContactsStore contactStore; private JsonThreadStore threadStore; + private SignalServiceMessagePipe messagePipe = null; public Manager(String username, String settingsPath) { this.username = username; @@ -217,7 +220,7 @@ class Manager implements Signal { migrateLegacyConfigs(); - accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, deviceId, USER_AGENT); + accountManager = new SignalServiceAccountManager(serviceUrls, username, password, deviceId, USER_AGENT); try { if (registered && accountManager.getPreKeysCount() < PREKEY_MINIMUM_COUNT) { refreshPreKeys(); @@ -342,7 +345,7 @@ class Manager implements Signal { public void register(boolean voiceVerification) throws IOException { password = Util.getSecret(18); - accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT); + accountManager = new SignalServiceAccountManager(serviceUrls, username, password, USER_AGENT); if (voiceVerification) accountManager.requestVoiceVerificationCode(); @@ -353,10 +356,21 @@ class Manager implements Signal { save(); } + public void updateAccountAttributes() throws IOException { + accountManager.setAccountAttributes(signalingKey, signalProtocolStore.getLocalRegistrationId(), false, true); + } + + public void unregister() throws IOException { + // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false. + // If this is the master device, other users can't send messages to this number anymore. + // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore. + accountManager.setGcmId(Optional.absent()); + } + public URI getDeviceLinkUri() throws TimeoutException, IOException { password = Util.getSecret(18); - accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT); + accountManager = new SignalServiceAccountManager(serviceUrls, username, password, USER_AGENT); String uuid = accountManager.getNewDeviceUuid(); registered = false; @@ -783,8 +797,8 @@ class Manager implements Signal { private void sendSyncMessage(SignalServiceSyncMessage message) throws IOException, UntrustedIdentityException { - SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password, - deviceId, signalProtocolStore, USER_AGENT, Optional.absent()); + SignalServiceMessageSender messageSender = new SignalServiceMessageSender(serviceUrls, username, password, + deviceId, signalProtocolStore, USER_AGENT, Optional.fromNullable(messagePipe), Optional.absent()); try { messageSender.sendMessage(message); } catch (UntrustedIdentityException e) { @@ -800,8 +814,8 @@ class Manager implements Signal { SignalServiceDataMessage message = null; try { - SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password, - deviceId, signalProtocolStore, USER_AGENT, Optional.absent()); + SignalServiceMessageSender messageSender = new SignalServiceMessageSender(serviceUrls, username, password, + deviceId, signalProtocolStore, USER_AGENT, Optional.fromNullable(messagePipe), Optional.absent()); message = messageBuilder.build(); if (message.getGroupInfo().isPresent()) { @@ -1029,11 +1043,12 @@ class Manager implements Signal { public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, boolean ignoreAttachments, ReceiveMessageHandler handler) throws IOException { retryFailedReceivedMessages(handler, ignoreAttachments); - final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT); - SignalServiceMessagePipe messagePipe = null; + final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT); try { - messagePipe = messageReceiver.createMessagePipe(); + if (messagePipe == null) { + messagePipe = messageReceiver.createMessagePipe(); + } while (true) { SignalServiceEnvelope envelope; @@ -1082,8 +1097,10 @@ class Manager implements Signal { } } } finally { - if (messagePipe != null) + if (messagePipe != null) { messagePipe.shutdown(); + messagePipe = null; + } } } @@ -1314,7 +1331,7 @@ class Manager implements Signal { } } - final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT); + final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT); File tmpFile = Util.createTempFile(); try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile)) { @@ -1340,7 +1357,7 @@ class Manager implements Signal { } private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException { - final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT); + final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceUrls, username, password, deviceId, signalingKey, USER_AGENT); return messageReceiver.retrieveAttachment(pointer, tmpFile); }