X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/eabd361405a54a5b7122bf537cb299306f098e45..9d18b01d85b0588a03e4640213bb27ae174e7426:/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 94bb555a..d1963cb5 100644 --- a/src/main/java/org/asamk/signal/Manager.java +++ b/src/main/java/org/asamk/signal/Manager.java @@ -41,6 +41,7 @@ import org.whispersystems.signalservice.api.crypto.SignalServiceCipher; import org.whispersystems.signalservice.api.messages.*; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.TrustStore; +import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions; import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; @@ -58,7 +59,10 @@ class Manager implements Signal { public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle(); public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion(); - private final static String USER_AGENT = PROJECT_NAME + " " + PROJECT_VERSION; + private final static String USER_AGENT = PROJECT_NAME == null ? null : PROJECT_NAME + " " + PROJECT_VERSION; + + private final static int PREKEY_MINIMUM_COUNT = 20; + private static final int PREKEY_BATCH_SIZE = 100; private final String settingsPath; private final String dataPath; @@ -140,6 +144,14 @@ class Manager implements Signal { groupStore = new JsonGroupStore(); } accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT); + try { + if (registered && accountManager.getPreKeysCount() < PREKEY_MINIMUM_COUNT) { + refreshPreKeys(); + save(); + } + } catch (AuthorizationFailedException e) { + System.err.println("Authorization failed, was the number registered elsewhere?"); + } } private void save() { @@ -187,12 +199,10 @@ class Manager implements Signal { save(); } - private static final int BATCH_SIZE = 100; - private List generatePreKeys() { List records = new LinkedList<>(); - for (int i = 0; i < BATCH_SIZE; i++) { + for (int i = 0; i < PREKEY_BATCH_SIZE; i++) { int preKeyId = (preKeyIdOffset + i) % Medium.MAX_VALUE; ECKeyPair keyPair = Curve.generateKeyPair(); PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair); @@ -201,13 +211,13 @@ class Manager implements Signal { records.add(record); } - preKeyIdOffset = (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE; + preKeyIdOffset = (preKeyIdOffset + PREKEY_BATCH_SIZE + 1) % Medium.MAX_VALUE; save(); return records; } - private PreKeyRecord generateLastResortPreKey() { + private PreKeyRecord getOrGenerateLastResortPreKey() { if (signalProtocolStore.containsPreKey(Medium.MAX_VALUE)) { try { return signalProtocolStore.loadPreKey(Medium.MAX_VALUE); @@ -249,14 +259,16 @@ class Manager implements Signal { //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID))); registered = true; - List oneTimePreKeys = generatePreKeys(); - - PreKeyRecord lastResortKey = generateLastResortPreKey(); + refreshPreKeys(); + save(); + } + private void refreshPreKeys() throws IOException { + List oneTimePreKeys = generatePreKeys(); + PreKeyRecord lastResortKey = getOrGenerateLastResortPreKey(); SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(signalProtocolStore.getIdentityKeyPair()); accountManager.setPreKeys(signalProtocolStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys); - save(); }