X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/184354ffb71ea643b62c01c8406402ea4f492ac1..c53bb132eb9759ee6541d037d2eeadc848c2c89b:/src/main/java/org/asamk/signal/manager/Manager.java diff --git a/src/main/java/org/asamk/signal/manager/Manager.java b/src/main/java/org/asamk/signal/manager/Manager.java index 1b703e42..202a7d44 100644 --- a/src/main/java/org/asamk/signal/manager/Manager.java +++ b/src/main/java/org/asamk/signal/manager/Manager.java @@ -56,6 +56,7 @@ import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.SleepTimer; import org.whispersystems.signalservice.api.util.UptimeSleepTimer; import org.whispersystems.signalservice.internal.push.SignalServiceProtos; +import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException; import org.whispersystems.signalservice.internal.util.Base64; import java.io.*; @@ -73,16 +74,14 @@ public class Manager implements Signal { private final String dataPath; private final String attachmentsPath; private final String avatarsPath; + private final SleepTimer timer = new UptimeSleepTimer(); private SignalAccount account; - private String username; private SignalServiceAccountManager accountManager; private SignalServiceMessagePipe messagePipe = null; private SignalServiceMessagePipe unidentifiedMessagePipe = null; - private SleepTimer timer = new UptimeSleepTimer(); - public Manager(String username, String settingsPath) { this.username = username; this.settingsPath = settingsPath; @@ -138,6 +137,7 @@ public class Manager implements Signal { } } catch (AuthorizationFailedException e) { System.err.println("Authorization failed, was the number registered elsewhere?"); + throw e; } } @@ -163,6 +163,7 @@ public class Manager implements Signal { if (account.getProfileKey() == null) { // Old config file, creating new profile key account.setProfileKey(KeyUtils.createProfileKey()); + account.save(); } } @@ -189,10 +190,11 @@ public class Manager implements Signal { account.setPassword(KeyUtils.createPassword()); accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, account.getUsername(), account.getPassword(), BaseConfig.USER_AGENT, timer); - if (voiceVerification) - accountManager.requestVoiceVerificationCode(); - else - accountManager.requestSmsVerificationCode(); + if (voiceVerification) { + accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.absent(), Optional.absent()); + } else { + accountManager.requestSmsVerificationCode(false, Optional.absent(), Optional.absent()); + } account.setRegistered(false); account.save(); @@ -207,6 +209,9 @@ public class Manager implements Signal { // 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()); + + account.setRegistered(false); + account.save(); } public String getDeviceLinkUri() throws TimeoutException, IOException { @@ -250,11 +255,15 @@ public class Manager implements Signal { public List getLinkedDevices() throws IOException { List devices = accountManager.getDevices(); account.setMultiDevice(devices.size() > 1); + account.save(); return devices; } public void removeLinkedDevices(int deviceId) throws IOException { accountManager.removeDevice(deviceId); + List devices = accountManager.getDevices(); + account.setMultiDevice(devices.size() > 1); + account.save(); } public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException { @@ -269,6 +278,7 @@ public class Manager implements Signal { accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, Optional.of(account.getProfileKey()), verificationCode); account.setMultiDevice(true); + account.save(); } private List generatePreKeys() { @@ -505,8 +515,15 @@ public class Manager implements Signal { } } - return SignalServiceDataMessage.newBuilder() + SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder() .asGroupMessage(group.build()); + + ThreadInfo thread = account.getThreadStore().getThread(Base64.encodeBytes(g.groupId)); + if (thread != null) { + messageBuilder.withExpiration(thread.messageExpirationTime); + } + + return messageBuilder; } private void sendGroupInfoRequest(byte[] groupId, String recipient) throws IOException, EncapsulatedExceptions { @@ -520,6 +537,11 @@ public class Manager implements Signal { SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder() .asGroupMessage(group.build()); + ThreadInfo thread = account.getThreadStore().getThread(Base64.encodeBytes(groupId)); + if (thread != null) { + messageBuilder.withExpiration(thread.messageExpirationTime); + } + // Send group info request message to the recipient who sent us a message with this groupId final List membersSend = new ArrayList<>(); membersSend.add(recipient); @@ -542,6 +564,7 @@ public class Manager implements Signal { if (attachments != null) { messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments)); } + messageBuilder.withProfileKey(account.getProfileKey()); sendMessageLegacy(messageBuilder, recipients); } @@ -757,7 +780,8 @@ public class Manager implements Signal { message = messageBuilder.build(); if (message.getGroupInfo().isPresent()) { try { - List result = messageSender.sendMessage(new ArrayList<>(recipientsTS), getAccessFor(recipientsTS), message); + final boolean isRecipientUpdate = false; + List result = messageSender.sendMessage(new ArrayList<>(recipientsTS), getAccessFor(recipientsTS), isRecipientUpdate, message); for (SendMessageResult r : result) { if (r.getIdentityFailure() != null) { account.getSignalProtocolStore().saveIdentity(r.getAddress().getNumber(), r.getIdentityFailure().getIdentityKey(), TrustLevel.UNTRUSTED); @@ -768,6 +792,25 @@ public class Manager implements Signal { account.getSignalProtocolStore().saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED); return Collections.emptyList(); } + } else if (recipientsTS.size() == 1 && recipientsTS.contains(new SignalServiceAddress(username))) { + SignalServiceAddress recipient = new SignalServiceAddress(username); + final Optional unidentifiedAccess = getAccessFor(recipient); + SentTranscriptMessage transcript = new SentTranscriptMessage(recipient.getNumber(), + message.getTimestamp(), + message, + message.getExpiresInSeconds(), + Collections.singletonMap(recipient.getNumber(), unidentifiedAccess.isPresent()), + false); + SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript); + + List results = new ArrayList<>(recipientsTS.size()); + try { + messageSender.sendMessage(syncMessage, unidentifiedAccess); + } catch (UntrustedIdentityException e) { + account.getSignalProtocolStore().saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED); + results.add(SendMessageResult.identityFailure(recipient, e.getIdentityKey())); + } + return results; } else { // Send to all individually, so sync messages are sent correctly List results = new ArrayList<>(recipientsTS.size()); @@ -799,7 +842,7 @@ public class Manager implements Signal { } } - private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, ProtocolUntrustedIdentityException, SelfSendException { + private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, ProtocolUntrustedIdentityException, SelfSendException, UnsupportedDataMessageException { SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(username), account.getSignalProtocolStore(), Utils.getCertificateValidator()); try { return cipher.decrypt(envelope); @@ -1144,6 +1187,10 @@ public class Manager implements Signal { } if (c.getExpirationTimer().isPresent()) { ThreadInfo thread = account.getThreadStore().getThread(c.getNumber()); + if (thread == null) { + thread = new ThreadInfo(); + thread.id = c.getNumber(); + } thread.messageExpirationTime = c.getExpirationTimer().get(); account.getThreadStore().updateThread(thread); }