2 Copyright (C) 2015-2018 AsamK
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
.manager
;
19 import org
.asamk
.Signal
;
20 import org
.asamk
.signal
.*;
21 import org
.asamk
.signal
.storage
.SignalAccount
;
22 import org
.asamk
.signal
.storage
.contacts
.ContactInfo
;
23 import org
.asamk
.signal
.storage
.groups
.GroupInfo
;
24 import org
.asamk
.signal
.storage
.groups
.JsonGroupStore
;
25 import org
.asamk
.signal
.storage
.protocol
.JsonIdentityKeyStore
;
26 import org
.asamk
.signal
.storage
.threads
.ThreadInfo
;
27 import org
.asamk
.signal
.util
.IOUtils
;
28 import org
.asamk
.signal
.util
.Util
;
29 import org
.signal
.libsignal
.metadata
.*;
30 import org
.whispersystems
.libsignal
.*;
31 import org
.whispersystems
.libsignal
.ecc
.Curve
;
32 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
33 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
34 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
35 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
36 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
37 import org
.whispersystems
.libsignal
.util
.Medium
;
38 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
39 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
40 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
41 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
42 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
43 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
44 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
45 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccessPair
;
46 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
47 import org
.whispersystems
.signalservice
.api
.messages
.*;
48 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.*;
49 import org
.whispersystems
.signalservice
.api
.push
.ContactTokenDetails
;
50 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
51 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
52 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
53 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NetworkFailureException
;
54 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
55 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
56 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
57 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
58 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
59 import org
.whispersystems
.signalservice
.internal
.util
.Base64
;
63 import java
.nio
.file
.Files
;
64 import java
.nio
.file
.Paths
;
65 import java
.nio
.file
.StandardCopyOption
;
67 import java
.util
.concurrent
.TimeUnit
;
68 import java
.util
.concurrent
.TimeoutException
;
70 public class Manager
implements Signal
{
72 private final String settingsPath
;
73 private final String dataPath
;
74 private final String attachmentsPath
;
75 private final String avatarsPath
;
76 private final SleepTimer timer
= new UptimeSleepTimer();
78 private SignalAccount account
;
79 private String username
;
80 private SignalServiceAccountManager accountManager
;
81 private SignalServiceMessagePipe messagePipe
= null;
82 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
84 public Manager(String username
, String settingsPath
) {
85 this.username
= username
;
86 this.settingsPath
= settingsPath
;
87 this.dataPath
= this.settingsPath
+ "/data";
88 this.attachmentsPath
= this.settingsPath
+ "/attachments";
89 this.avatarsPath
= this.settingsPath
+ "/avatars";
93 public String
getUsername() {
97 private IdentityKey
getIdentity() {
98 return account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey();
101 public int getDeviceId() {
102 return account
.getDeviceId();
105 private String
getMessageCachePath() {
106 return this.dataPath
+ "/" + username
+ ".d/msg-cache";
109 private String
getMessageCachePath(String sender
) {
110 return getMessageCachePath() + "/" + sender
.replace("/", "_");
113 private File
getMessageCacheFile(String sender
, long now
, long timestamp
) throws IOException
{
114 String cachePath
= getMessageCachePath(sender
);
115 IOUtils
.createPrivateDirectories(cachePath
);
116 return new File(cachePath
+ "/" + now
+ "_" + timestamp
);
119 public boolean userHasKeys() {
120 return account
!= null && account
.getSignalProtocolStore() != null;
123 public void init() throws IOException
{
124 if (!SignalAccount
.userExists(dataPath
, username
)) {
127 account
= SignalAccount
.load(dataPath
, username
);
129 migrateLegacyConfigs();
131 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), BaseConfig
.USER_AGENT
, timer
);
133 if (account
.isRegistered() && accountManager
.getPreKeysCount() < BaseConfig
.PREKEY_MINIMUM_COUNT
) {
137 } catch (AuthorizationFailedException e
) {
138 System
.err
.println("Authorization failed, was the number registered elsewhere?");
142 private void migrateLegacyConfigs() {
143 // Copy group avatars that were previously stored in the attachments folder
144 // to the new avatar folder
145 if (JsonGroupStore
.groupsWithLegacyAvatarId
.size() > 0) {
146 for (GroupInfo g
: JsonGroupStore
.groupsWithLegacyAvatarId
) {
147 File avatarFile
= getGroupAvatarFile(g
.groupId
);
148 File attachmentFile
= getAttachmentFile(g
.getAvatarId());
149 if (!avatarFile
.exists() && attachmentFile
.exists()) {
151 IOUtils
.createPrivateDirectories(avatarsPath
);
152 Files
.copy(attachmentFile
.toPath(), avatarFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
153 } catch (Exception e
) {
158 JsonGroupStore
.groupsWithLegacyAvatarId
.clear();
161 if (account
.getProfileKey() == null) {
162 // Old config file, creating new profile key
163 account
.setProfileKey(KeyUtils
.createProfileKey());
167 private void createNewIdentity() throws IOException
{
168 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
169 int registrationId
= KeyHelper
.generateRegistrationId(false);
170 if (username
== null) {
171 account
= SignalAccount
.createTemporaryAccount(identityKey
, registrationId
);
173 byte[] profileKey
= KeyUtils
.createProfileKey();
174 account
= SignalAccount
.create(dataPath
, username
, identityKey
, registrationId
, profileKey
);
179 public boolean isRegistered() {
180 return account
!= null && account
.isRegistered();
183 public void register(boolean voiceVerification
) throws IOException
{
184 if (account
== null) {
187 account
.setPassword(KeyUtils
.createPassword());
188 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, account
.getUsername(), account
.getPassword(), BaseConfig
.USER_AGENT
, timer
);
190 if (voiceVerification
)
191 accountManager
.requestVoiceVerificationCode();
193 accountManager
.requestSmsVerificationCode();
195 account
.setRegistered(false);
199 public void updateAccountAttributes() throws IOException
{
200 accountManager
.setAccountAttributes(account
.getSignalingKey(), account
.getSignalProtocolStore().getLocalRegistrationId(), true, account
.getRegistrationLockPin(), getSelfUnidentifiedAccessKey(), false);
203 public void unregister() throws IOException
{
204 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
205 // If this is the master device, other users can't send messages to this number anymore.
206 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
207 accountManager
.setGcmId(Optional
.<String
>absent());
210 public String
getDeviceLinkUri() throws TimeoutException
, IOException
{
211 if (account
== null) {
214 account
.setPassword(KeyUtils
.createPassword());
215 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), BaseConfig
.USER_AGENT
, timer
);
216 String uuid
= accountManager
.getNewDeviceUuid();
218 return Utils
.createDeviceLinkUri(new Utils
.DeviceLinkInfo(uuid
, getIdentity().getPublicKey()));
221 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
222 account
.setSignalingKey(KeyUtils
.createSignalingKey());
223 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(account
.getSignalProtocolStore().getIdentityKeyPair(), account
.getSignalingKey(), false, true, account
.getSignalProtocolStore().getLocalRegistrationId(), deviceName
);
225 username
= ret
.getNumber();
226 // TODO do this check before actually registering
227 if (SignalAccount
.userExists(dataPath
, username
)) {
228 throw new UserAlreadyExists(username
, SignalAccount
.getFileName(dataPath
, username
));
231 // Create new account with the synced identity
232 byte[] profileKey
= ret
.getProfileKey();
233 if (profileKey
== null) {
234 profileKey
= KeyUtils
.createProfileKey();
236 account
= SignalAccount
.createLinkedAccount(dataPath
, username
, account
.getPassword(), ret
.getDeviceId(), ret
.getIdentity(), account
.getSignalProtocolStore().getLocalRegistrationId(), account
.getSignalingKey(), profileKey
);
241 requestSyncContacts();
242 requestSyncBlocked();
243 requestSyncConfiguration();
248 public List
<DeviceInfo
> getLinkedDevices() throws IOException
{
249 List
<DeviceInfo
> devices
= accountManager
.getDevices();
250 account
.setMultiDevice(devices
.size() > 1);
255 public void removeLinkedDevices(int deviceId
) throws IOException
{
256 accountManager
.removeDevice(deviceId
);
257 List
<DeviceInfo
> devices
= accountManager
.getDevices();
258 account
.setMultiDevice(devices
.size() > 1);
262 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
263 Utils
.DeviceLinkInfo info
= Utils
.parseDeviceLinkUri(linkUri
);
265 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
268 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
269 IdentityKeyPair identityKeyPair
= account
.getSignalProtocolStore().getIdentityKeyPair();
270 String verificationCode
= accountManager
.getNewDeviceVerificationCode();
272 accountManager
.addDevice(deviceIdentifier
, deviceKey
, identityKeyPair
, Optional
.of(account
.getProfileKey()), verificationCode
);
273 account
.setMultiDevice(true);
277 private List
<PreKeyRecord
> generatePreKeys() {
278 List
<PreKeyRecord
> records
= new ArrayList
<>(BaseConfig
.PREKEY_BATCH_SIZE
);
280 final int offset
= account
.getPreKeyIdOffset();
281 for (int i
= 0; i
< BaseConfig
.PREKEY_BATCH_SIZE
; i
++) {
282 int preKeyId
= (offset
+ i
) % Medium
.MAX_VALUE
;
283 ECKeyPair keyPair
= Curve
.generateKeyPair();
284 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
289 account
.addPreKeys(records
);
295 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
297 ECKeyPair keyPair
= Curve
.generateKeyPair();
298 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
299 SignedPreKeyRecord
record = new SignedPreKeyRecord(account
.getNextSignedPreKeyId(), System
.currentTimeMillis(), keyPair
, signature
);
301 account
.addSignedPreKey(record);
305 } catch (InvalidKeyException e
) {
306 throw new AssertionError(e
);
310 public void verifyAccount(String verificationCode
, String pin
) throws IOException
{
311 verificationCode
= verificationCode
.replace("-", "");
312 account
.setSignalingKey(KeyUtils
.createSignalingKey());
313 // TODO make unrestricted unidentified access configurable
314 accountManager
.verifyAccountWithCode(verificationCode
, account
.getSignalingKey(), account
.getSignalProtocolStore().getLocalRegistrationId(), true, pin
, getSelfUnidentifiedAccessKey(), false);
316 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
317 account
.setRegistered(true);
318 account
.setRegistrationLockPin(pin
);
324 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
{
325 accountManager
.setPin(pin
);
326 if (pin
.isPresent()) {
327 account
.setRegistrationLockPin(pin
.get());
329 account
.setRegistrationLockPin(null);
334 private void refreshPreKeys() throws IOException
{
335 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
336 final IdentityKeyPair identityKeyPair
= account
.getSignalProtocolStore().getIdentityKeyPair();
337 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
339 accountManager
.setPreKeys(getIdentity(), signedPreKeyRecord
, oneTimePreKeys
);
342 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(byte[] groupId
) throws IOException
{
343 File file
= getGroupAvatarFile(groupId
);
344 if (!file
.exists()) {
345 return Optional
.absent();
348 return Optional
.of(Utils
.createAttachment(file
));
351 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(String number
) throws IOException
{
352 File file
= getContactAvatarFile(number
);
353 if (!file
.exists()) {
354 return Optional
.absent();
357 return Optional
.of(Utils
.createAttachment(file
));
360 private GroupInfo
getGroupForSending(byte[] groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
361 GroupInfo g
= account
.getGroupStore().getGroup(groupId
);
363 throw new GroupNotFoundException(groupId
);
365 for (String member
: g
.members
) {
366 if (member
.equals(this.username
)) {
370 throw new NotAGroupMemberException(groupId
, g
.name
);
373 public List
<GroupInfo
> getGroups() {
374 return account
.getGroupStore().getGroups();
378 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
380 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
381 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
382 if (attachments
!= null) {
383 messageBuilder
.withAttachments(Utils
.getSignalServiceAttachments(attachments
));
385 if (groupId
!= null) {
386 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
389 messageBuilder
.asGroupMessage(group
);
391 ThreadInfo thread
= account
.getThreadStore().getThread(Base64
.encodeBytes(groupId
));
392 if (thread
!= null) {
393 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
396 final GroupInfo g
= getGroupForSending(groupId
);
398 // Don't send group message to ourself
399 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
400 membersSend
.remove(this.username
);
401 sendMessageLegacy(messageBuilder
, membersSend
);
404 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
{
405 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
409 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
410 .asGroupMessage(group
);
412 final GroupInfo g
= getGroupForSending(groupId
);
413 g
.members
.remove(this.username
);
414 account
.getGroupStore().updateGroup(g
);
416 sendMessageLegacy(messageBuilder
, g
.members
);
419 private byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
421 if (groupId
== null) {
423 g
= new GroupInfo(KeyUtils
.createGroupId());
424 g
.members
.add(username
);
426 g
= getGroupForSending(groupId
);
433 if (members
!= null) {
434 Set
<String
> newMembers
= new HashSet
<>();
435 for (String member
: members
) {
437 member
= Utils
.canonicalizeNumber(member
, username
);
438 } catch (InvalidNumberException e
) {
439 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
440 System
.err
.println("Aborting…");
443 if (g
.members
.contains(member
)) {
446 newMembers
.add(member
);
447 g
.members
.add(member
);
449 final List
<ContactTokenDetails
> contacts
= accountManager
.getContacts(newMembers
);
450 if (contacts
.size() != newMembers
.size()) {
451 // Some of the new members are not registered on Signal
452 for (ContactTokenDetails contact
: contacts
) {
453 newMembers
.remove(contact
.getNumber());
455 System
.err
.println("Failed to add members " + Util
.join(", ", newMembers
) + " to group: Not registered on Signal");
456 System
.err
.println("Aborting…");
461 if (avatarFile
!= null) {
462 IOUtils
.createPrivateDirectories(avatarsPath
);
463 File aFile
= getGroupAvatarFile(g
.groupId
);
464 Files
.copy(Paths
.get(avatarFile
), aFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
467 account
.getGroupStore().updateGroup(g
);
469 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
471 // Don't send group message to ourself
472 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
473 membersSend
.remove(this.username
);
474 sendMessageLegacy(messageBuilder
, membersSend
);
478 private void sendUpdateGroupMessage(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
479 if (groupId
== null) {
482 GroupInfo g
= getGroupForSending(groupId
);
484 if (!g
.members
.contains(recipient
)) {
488 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
490 // Send group message only to the recipient who requested it
491 final List
<String
> membersSend
= new ArrayList
<>();
492 membersSend
.add(recipient
);
493 sendMessageLegacy(messageBuilder
, membersSend
);
496 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfo g
) {
497 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
500 .withMembers(new ArrayList
<>(g
.members
));
502 File aFile
= getGroupAvatarFile(g
.groupId
);
503 if (aFile
.exists()) {
505 group
.withAvatar(Utils
.createAttachment(aFile
));
506 } catch (IOException e
) {
507 throw new AttachmentInvalidException(aFile
.toString(), e
);
511 return SignalServiceDataMessage
.newBuilder()
512 .asGroupMessage(group
.build());
515 private void sendGroupInfoRequest(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
516 if (groupId
== null) {
520 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
)
523 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
524 .asGroupMessage(group
.build());
526 // Send group info request message to the recipient who sent us a message with this groupId
527 final List
<String
> membersSend
= new ArrayList
<>();
528 membersSend
.add(recipient
);
529 sendMessageLegacy(messageBuilder
, membersSend
);
533 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
534 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
{
535 List
<String
> recipients
= new ArrayList
<>(1);
536 recipients
.add(recipient
);
537 sendMessage(message
, attachments
, recipients
);
541 public void sendMessage(String messageText
, List
<String
> attachments
,
542 List
<String
> recipients
)
543 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
{
544 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
545 if (attachments
!= null) {
546 messageBuilder
.withAttachments(Utils
.getSignalServiceAttachments(attachments
));
548 sendMessageLegacy(messageBuilder
, recipients
);
552 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
{
553 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
554 .asEndSessionMessage();
556 sendMessageLegacy(messageBuilder
, recipients
);
560 public String
getContactName(String number
) {
561 ContactInfo contact
= account
.getContactStore().getContact(number
);
562 if (contact
== null) {
570 public void setContactName(String number
, String name
) {
571 ContactInfo contact
= account
.getContactStore().getContact(number
);
572 if (contact
== null) {
573 contact
= new ContactInfo();
574 contact
.number
= number
;
575 System
.err
.println("Add contact " + number
+ " named " + name
);
577 System
.err
.println("Updating contact " + number
+ " name " + contact
.name
+ " -> " + name
);
580 account
.getContactStore().updateContact(contact
);
585 public List
<byte[]> getGroupIds() {
586 List
<GroupInfo
> groups
= getGroups();
587 List
<byte[]> ids
= new ArrayList
<>(groups
.size());
588 for (GroupInfo group
: groups
) {
589 ids
.add(group
.groupId
);
595 public String
getGroupName(byte[] groupId
) {
596 GroupInfo group
= getGroup(groupId
);
605 public List
<String
> getGroupMembers(byte[] groupId
) {
606 GroupInfo group
= getGroup(groupId
);
608 return new ArrayList
<>();
610 return new ArrayList
<>(group
.members
);
615 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
616 if (groupId
.length
== 0) {
619 if (name
.isEmpty()) {
622 if (members
.size() == 0) {
625 if (avatar
.isEmpty()) {
628 return sendUpdateGroupMessage(groupId
, name
, members
, avatar
);
632 * Change the expiration timer for a thread (number of groupId)
634 * @param numberOrGroupId
635 * @param messageExpirationTimer
637 public void setExpirationTimer(String numberOrGroupId
, int messageExpirationTimer
) {
638 ThreadInfo thread
= account
.getThreadStore().getThread(numberOrGroupId
);
639 thread
.messageExpirationTime
= messageExpirationTimer
;
640 account
.getThreadStore().updateThread(thread
);
643 private void requestSyncGroups() throws IOException
{
644 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
).build();
645 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
647 sendSyncMessage(message
);
648 } catch (UntrustedIdentityException e
) {
653 private void requestSyncContacts() throws IOException
{
654 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
).build();
655 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
657 sendSyncMessage(message
);
658 } catch (UntrustedIdentityException e
) {
663 private void requestSyncBlocked() throws IOException
{
664 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
).build();
665 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
667 sendSyncMessage(message
);
668 } catch (UntrustedIdentityException e
) {
673 private void requestSyncConfiguration() throws IOException
{
674 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
).build();
675 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
677 sendSyncMessage(message
);
678 } catch (UntrustedIdentityException e
) {
683 private byte[] getSelfUnidentifiedAccessKey() {
684 return UnidentifiedAccess
.deriveAccessKeyFrom(account
.getProfileKey());
687 private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient
) {
692 private Optional
<UnidentifiedAccessPair
> getAccessForSync() {
694 return Optional
.absent();
697 private List
<Optional
<UnidentifiedAccessPair
>> getAccessFor(Collection
<SignalServiceAddress
> recipients
) {
698 List
<Optional
<UnidentifiedAccessPair
>> result
= new ArrayList
<>(recipients
.size());
699 for (SignalServiceAddress recipient
: recipients
) {
700 result
.add(Optional
.<UnidentifiedAccessPair
>absent());
705 private Optional
<UnidentifiedAccessPair
> getAccessFor(SignalServiceAddress recipient
) {
707 return Optional
.absent();
710 private void sendSyncMessage(SignalServiceSyncMessage message
)
711 throws IOException
, UntrustedIdentityException
{
712 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(),
713 account
.getDeviceId(), account
.getSignalProtocolStore(), BaseConfig
.USER_AGENT
, account
.isMultiDevice(), Optional
.fromNullable(messagePipe
), Optional
.fromNullable(unidentifiedMessagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
715 messageSender
.sendMessage(message
, getAccessForSync());
716 } catch (UntrustedIdentityException e
) {
717 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
723 * This method throws an EncapsulatedExceptions exception instead of returning a list of SendMessageResult.
725 private void sendMessageLegacy(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
726 throws EncapsulatedExceptions
, IOException
{
727 List
<SendMessageResult
> results
= sendMessage(messageBuilder
, recipients
);
729 List
<UntrustedIdentityException
> untrustedIdentities
= new LinkedList
<>();
730 List
<UnregisteredUserException
> unregisteredUsers
= new LinkedList
<>();
731 List
<NetworkFailureException
> networkExceptions
= new LinkedList
<>();
733 for (SendMessageResult result
: results
) {
734 if (result
.isUnregisteredFailure()) {
735 unregisteredUsers
.add(new UnregisteredUserException(result
.getAddress().getNumber(), null));
736 } else if (result
.isNetworkFailure()) {
737 networkExceptions
.add(new NetworkFailureException(result
.getAddress().getNumber(), null));
738 } else if (result
.getIdentityFailure() != null) {
739 untrustedIdentities
.add(new UntrustedIdentityException("Untrusted", result
.getAddress().getNumber(), result
.getIdentityFailure().getIdentityKey()));
742 if (!untrustedIdentities
.isEmpty() || !unregisteredUsers
.isEmpty() || !networkExceptions
.isEmpty()) {
743 throw new EncapsulatedExceptions(untrustedIdentities
, unregisteredUsers
, networkExceptions
);
747 private List
<SendMessageResult
> sendMessage(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
749 Set
<SignalServiceAddress
> recipientsTS
= Utils
.getSignalServiceAddresses(recipients
, username
);
750 if (recipientsTS
== null) {
752 return Collections
.emptyList();
755 SignalServiceDataMessage message
= null;
757 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(),
758 account
.getDeviceId(), account
.getSignalProtocolStore(), BaseConfig
.USER_AGENT
, account
.isMultiDevice(), Optional
.fromNullable(messagePipe
), Optional
.fromNullable(unidentifiedMessagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
760 message
= messageBuilder
.build();
761 if (message
.getGroupInfo().isPresent()) {
763 List
<SendMessageResult
> result
= messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), getAccessFor(recipientsTS
), message
);
764 for (SendMessageResult r
: result
) {
765 if (r
.getIdentityFailure() != null) {
766 account
.getSignalProtocolStore().saveIdentity(r
.getAddress().getNumber(), r
.getIdentityFailure().getIdentityKey(), TrustLevel
.UNTRUSTED
);
770 } catch (UntrustedIdentityException e
) {
771 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
772 return Collections
.emptyList();
775 // Send to all individually, so sync messages are sent correctly
776 List
<SendMessageResult
> results
= new ArrayList
<>(recipientsTS
.size());
777 for (SignalServiceAddress address
: recipientsTS
) {
778 ThreadInfo thread
= account
.getThreadStore().getThread(address
.getNumber());
779 if (thread
!= null) {
780 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
782 messageBuilder
.withExpiration(0);
784 message
= messageBuilder
.build();
786 SendMessageResult result
= messageSender
.sendMessage(address
, getAccessFor(address
), message
);
788 } catch (UntrustedIdentityException e
) {
789 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
790 results
.add(SendMessageResult
.identityFailure(address
, e
.getIdentityKey()));
796 if (message
!= null && message
.isEndSession()) {
797 for (SignalServiceAddress recipient
: recipientsTS
) {
798 handleEndSession(recipient
.getNumber());
805 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, ProtocolUntrustedIdentityException
, SelfSendException
{
806 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), account
.getSignalProtocolStore(), Utils
.getCertificateValidator());
808 return cipher
.decrypt(envelope
);
809 } catch (ProtocolUntrustedIdentityException e
) {
810 // TODO We don't get the new untrusted identity from ProtocolUntrustedIdentityException anymore ... we need to get it from somewhere else
811 // account.getSignalProtocolStore().saveIdentity(e.getSender(), e.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
816 private void handleEndSession(String source
) {
817 account
.getSignalProtocolStore().deleteAllSessions(source
);
820 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
, boolean ignoreAttachments
) {
822 if (message
.getGroupInfo().isPresent()) {
823 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
824 threadId
= Base64
.encodeBytes(groupInfo
.getGroupId());
825 GroupInfo group
= account
.getGroupStore().getGroup(groupInfo
.getGroupId());
826 switch (groupInfo
.getType()) {
829 group
= new GroupInfo(groupInfo
.getGroupId());
832 if (groupInfo
.getAvatar().isPresent()) {
833 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
834 if (avatar
.isPointer()) {
836 retrieveGroupAvatarAttachment(avatar
.asPointer(), group
.groupId
);
837 } catch (IOException
| InvalidMessageException e
) {
838 System
.err
.println("Failed to retrieve group avatar (" + avatar
.asPointer().getId() + "): " + e
.getMessage());
843 if (groupInfo
.getName().isPresent()) {
844 group
.name
= groupInfo
.getName().get();
847 if (groupInfo
.getMembers().isPresent()) {
848 group
.members
.addAll(groupInfo
.getMembers().get());
851 account
.getGroupStore().updateGroup(group
);
856 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
857 } catch (IOException
| EncapsulatedExceptions e
) {
865 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
866 } catch (IOException
| EncapsulatedExceptions e
) {
870 group
.members
.remove(source
);
871 account
.getGroupStore().updateGroup(group
);
877 sendUpdateGroupMessage(groupInfo
.getGroupId(), source
);
878 } catch (IOException
| EncapsulatedExceptions e
) {
880 } catch (NotAGroupMemberException e
) {
881 // We have left this group, so don't send a group update message
888 threadId
= destination
;
893 if (message
.isEndSession()) {
894 handleEndSession(isSync ? destination
: source
);
896 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
897 ThreadInfo thread
= account
.getThreadStore().getThread(threadId
);
898 if (thread
== null) {
899 thread
= new ThreadInfo();
900 thread
.id
= threadId
;
902 if (thread
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
903 thread
.messageExpirationTime
= message
.getExpiresInSeconds();
904 account
.getThreadStore().updateThread(thread
);
907 if (message
.getAttachments().isPresent() && !ignoreAttachments
) {
908 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
909 if (attachment
.isPointer()) {
911 retrieveAttachment(attachment
.asPointer());
912 } catch (IOException
| InvalidMessageException e
) {
913 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
918 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
919 if (source
.equals(username
)) {
920 this.account
.setProfileKey(message
.getProfileKey().get());
922 ContactInfo contact
= account
.getContactStore().getContact(source
);
923 if (contact
== null) {
924 contact
= new ContactInfo();
925 contact
.number
= source
;
927 contact
.profileKey
= Base64
.encodeBytes(message
.getProfileKey().get());
931 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
932 final File cachePath
= new File(getMessageCachePath());
933 if (!cachePath
.exists()) {
936 for (final File dir
: Objects
.requireNonNull(cachePath
.listFiles())) {
937 if (!dir
.isDirectory()) {
941 for (final File fileEntry
: Objects
.requireNonNull(dir
.listFiles())) {
942 if (!fileEntry
.isFile()) {
945 SignalServiceEnvelope envelope
;
947 envelope
= Utils
.loadEnvelope(fileEntry
);
948 if (envelope
== null) {
951 } catch (IOException e
) {
955 SignalServiceContent content
= null;
956 if (!envelope
.isReceipt()) {
958 content
= decryptMessage(envelope
);
959 } catch (Exception e
) {
962 handleMessage(envelope
, content
, ignoreAttachments
);
965 handler
.handleMessage(envelope
, content
, null);
967 Files
.delete(fileEntry
.toPath());
968 } catch (IOException e
) {
969 System
.err
.println("Failed to delete cached message file “" + fileEntry
+ "”: " + e
.getMessage());
972 // Try to delete directory if empty
977 public void receiveMessages(long timeout
, TimeUnit unit
, boolean returnOnTimeout
, boolean ignoreAttachments
, ReceiveMessageHandler handler
) throws IOException
{
978 retryFailedReceivedMessages(handler
, ignoreAttachments
);
979 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
982 if (messagePipe
== null) {
983 messagePipe
= messageReceiver
.createMessagePipe();
987 SignalServiceEnvelope envelope
;
988 SignalServiceContent content
= null;
989 Exception exception
= null;
990 final long now
= new Date().getTime();
992 envelope
= messagePipe
.read(timeout
, unit
, new SignalServiceMessagePipe
.MessagePipeCallback() {
994 public void onMessage(SignalServiceEnvelope envelope
) {
995 // store message on disk, before acknowledging receipt to the server
997 File cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
998 Utils
.storeEnvelope(envelope
, cacheFile
);
999 } catch (IOException e
) {
1000 System
.err
.println("Failed to store encrypted message in disk cache, ignoring: " + e
.getMessage());
1004 } catch (TimeoutException e
) {
1005 if (returnOnTimeout
)
1008 } catch (InvalidVersionException e
) {
1009 System
.err
.println("Ignoring error: " + e
.getMessage());
1012 if (!envelope
.isReceipt()) {
1014 content
= decryptMessage(envelope
);
1015 } catch (Exception e
) {
1018 handleMessage(envelope
, content
, ignoreAttachments
);
1021 handler
.handleMessage(envelope
, content
, exception
);
1022 if (!(exception
instanceof ProtocolUntrustedIdentityException
)) {
1023 File cacheFile
= null;
1025 cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1026 Files
.delete(cacheFile
.toPath());
1027 // Try to delete directory if empty
1028 new File(getMessageCachePath()).delete();
1029 } catch (IOException e
) {
1030 System
.err
.println("Failed to delete cached message file “" + cacheFile
+ "”: " + e
.getMessage());
1035 if (messagePipe
!= null) {
1036 messagePipe
.shutdown();
1042 private void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
) {
1043 if (content
!= null) {
1044 if (content
.getDataMessage().isPresent()) {
1045 SignalServiceDataMessage message
= content
.getDataMessage().get();
1046 handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
, ignoreAttachments
);
1048 if (content
.getSyncMessage().isPresent()) {
1049 account
.setMultiDevice(true);
1050 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
1051 if (syncMessage
.getSent().isPresent()) {
1052 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
1053 handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get(), ignoreAttachments
);
1055 if (syncMessage
.getRequest().isPresent()) {
1056 RequestMessage rm
= syncMessage
.getRequest().get();
1057 if (rm
.isContactsRequest()) {
1060 } catch (UntrustedIdentityException
| IOException e
) {
1061 e
.printStackTrace();
1064 if (rm
.isGroupsRequest()) {
1067 } catch (UntrustedIdentityException
| IOException e
) {
1068 e
.printStackTrace();
1071 // TODO Handle rm.isBlockedListRequest(); rm.isConfigurationRequest();
1073 if (syncMessage
.getGroups().isPresent()) {
1074 File tmpFile
= null;
1076 tmpFile
= IOUtils
.createTempFile();
1077 try (InputStream attachmentAsStream
= retrieveAttachmentAsStream(syncMessage
.getGroups().get().asPointer(), tmpFile
)) {
1078 DeviceGroupsInputStream s
= new DeviceGroupsInputStream(attachmentAsStream
);
1080 while ((g
= s
.read()) != null) {
1081 GroupInfo syncGroup
= account
.getGroupStore().getGroup(g
.getId());
1082 if (syncGroup
== null) {
1083 syncGroup
= new GroupInfo(g
.getId());
1085 if (g
.getName().isPresent()) {
1086 syncGroup
.name
= g
.getName().get();
1088 syncGroup
.members
.addAll(g
.getMembers());
1089 syncGroup
.active
= g
.isActive();
1090 if (g
.getColor().isPresent()) {
1091 syncGroup
.color
= g
.getColor().get();
1094 if (g
.getAvatar().isPresent()) {
1095 retrieveGroupAvatarAttachment(g
.getAvatar().get(), syncGroup
.groupId
);
1097 account
.getGroupStore().updateGroup(syncGroup
);
1100 } catch (Exception e
) {
1101 e
.printStackTrace();
1103 if (tmpFile
!= null) {
1105 Files
.delete(tmpFile
.toPath());
1106 } catch (IOException e
) {
1107 System
.err
.println("Failed to delete received groups temp file “" + tmpFile
+ "”: " + e
.getMessage());
1112 if (syncMessage
.getBlockedList().isPresent()) {
1113 // TODO store list of blocked numbers
1115 if (syncMessage
.getContacts().isPresent()) {
1116 File tmpFile
= null;
1118 tmpFile
= IOUtils
.createTempFile();
1119 final ContactsMessage contactsMessage
= syncMessage
.getContacts().get();
1120 try (InputStream attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream().asPointer(), tmpFile
)) {
1121 DeviceContactsInputStream s
= new DeviceContactsInputStream(attachmentAsStream
);
1122 if (contactsMessage
.isComplete()) {
1123 account
.getContactStore().clear();
1126 while ((c
= s
.read()) != null) {
1127 if (c
.getNumber().equals(account
.getUsername()) && c
.getProfileKey().isPresent()) {
1128 account
.setProfileKey(c
.getProfileKey().get());
1130 ContactInfo contact
= account
.getContactStore().getContact(c
.getNumber());
1131 if (contact
== null) {
1132 contact
= new ContactInfo();
1133 contact
.number
= c
.getNumber();
1135 if (c
.getName().isPresent()) {
1136 contact
.name
= c
.getName().get();
1138 if (c
.getColor().isPresent()) {
1139 contact
.color
= c
.getColor().get();
1141 if (c
.getProfileKey().isPresent()) {
1142 contact
.profileKey
= Base64
.encodeBytes(c
.getProfileKey().get());
1144 if (c
.getVerified().isPresent()) {
1145 final VerifiedMessage verifiedMessage
= c
.getVerified().get();
1146 account
.getSignalProtocolStore().saveIdentity(verifiedMessage
.getDestination(), verifiedMessage
.getIdentityKey(), TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
1148 if (c
.getExpirationTimer().isPresent()) {
1149 ThreadInfo thread
= account
.getThreadStore().getThread(c
.getNumber());
1150 thread
.messageExpirationTime
= c
.getExpirationTimer().get();
1151 account
.getThreadStore().updateThread(thread
);
1153 if (c
.isBlocked()) {
1154 // TODO store list of blocked numbers
1156 account
.getContactStore().updateContact(contact
);
1158 if (c
.getAvatar().isPresent()) {
1159 retrieveContactAvatarAttachment(c
.getAvatar().get(), contact
.number
);
1163 } catch (Exception e
) {
1164 e
.printStackTrace();
1166 if (tmpFile
!= null) {
1168 Files
.delete(tmpFile
.toPath());
1169 } catch (IOException e
) {
1170 System
.err
.println("Failed to delete received contacts temp file “" + tmpFile
+ "”: " + e
.getMessage());
1175 if (syncMessage
.getVerified().isPresent()) {
1176 final VerifiedMessage verifiedMessage
= syncMessage
.getVerified().get();
1177 account
.getSignalProtocolStore().saveIdentity(verifiedMessage
.getDestination(), verifiedMessage
.getIdentityKey(), TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
1179 if (syncMessage
.getConfiguration().isPresent()) {
1186 private File
getContactAvatarFile(String number
) {
1187 return new File(avatarsPath
, "contact-" + number
);
1190 private File
retrieveContactAvatarAttachment(SignalServiceAttachment attachment
, String number
) throws IOException
, InvalidMessageException
{
1191 IOUtils
.createPrivateDirectories(avatarsPath
);
1192 if (attachment
.isPointer()) {
1193 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1194 return retrieveAttachment(pointer
, getContactAvatarFile(number
), false);
1196 SignalServiceAttachmentStream stream
= attachment
.asStream();
1197 return Utils
.retrieveAttachment(stream
, getContactAvatarFile(number
));
1201 private File
getGroupAvatarFile(byte[] groupId
) {
1202 return new File(avatarsPath
, "group-" + Base64
.encodeBytes(groupId
).replace("/", "_"));
1205 private File
retrieveGroupAvatarAttachment(SignalServiceAttachment attachment
, byte[] groupId
) throws IOException
, InvalidMessageException
{
1206 IOUtils
.createPrivateDirectories(avatarsPath
);
1207 if (attachment
.isPointer()) {
1208 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1209 return retrieveAttachment(pointer
, getGroupAvatarFile(groupId
), false);
1211 SignalServiceAttachmentStream stream
= attachment
.asStream();
1212 return Utils
.retrieveAttachment(stream
, getGroupAvatarFile(groupId
));
1216 public File
getAttachmentFile(long attachmentId
) {
1217 return new File(attachmentsPath
, attachmentId
+ "");
1220 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
1221 IOUtils
.createPrivateDirectories(attachmentsPath
);
1222 return retrieveAttachment(pointer
, getAttachmentFile(pointer
.getId()), true);
1225 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
, File outputFile
, boolean storePreview
) throws IOException
, InvalidMessageException
{
1226 if (storePreview
&& pointer
.getPreview().isPresent()) {
1227 File previewFile
= new File(outputFile
+ ".preview");
1228 try (OutputStream output
= new FileOutputStream(previewFile
)) {
1229 byte[] preview
= pointer
.getPreview().get();
1230 output
.write(preview
, 0, preview
.length
);
1231 } catch (FileNotFoundException e
) {
1232 e
.printStackTrace();
1237 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
1239 File tmpFile
= IOUtils
.createTempFile();
1240 try (InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
, BaseConfig
.MAX_ATTACHMENT_SIZE
)) {
1241 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1242 byte[] buffer
= new byte[4096];
1245 while ((read
= input
.read(buffer
)) != -1) {
1246 output
.write(buffer
, 0, read
);
1248 } catch (FileNotFoundException e
) {
1249 e
.printStackTrace();
1254 Files
.delete(tmpFile
.toPath());
1255 } catch (IOException e
) {
1256 System
.err
.println("Failed to delete received attachment temp file “" + tmpFile
+ "”: " + e
.getMessage());
1262 private InputStream
retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer
, File tmpFile
) throws IOException
, InvalidMessageException
{
1263 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
1264 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, BaseConfig
.MAX_ATTACHMENT_SIZE
);
1268 public boolean isRemote() {
1272 private void sendGroups() throws IOException
, UntrustedIdentityException
{
1273 File groupsFile
= IOUtils
.createTempFile();
1276 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
1277 DeviceGroupsOutputStream out
= new DeviceGroupsOutputStream(fos
);
1278 for (GroupInfo
record : account
.getGroupStore().getGroups()) {
1279 ThreadInfo info
= account
.getThreadStore().getThread(Base64
.encodeBytes(record.groupId
));
1280 out
.write(new DeviceGroup(record.groupId
, Optional
.fromNullable(record.name
),
1281 new ArrayList
<>(record.members
), createGroupAvatarAttachment(record.groupId
),
1282 record.active
, Optional
.fromNullable(info
!= null ? info
.messageExpirationTime
: null),
1283 Optional
.fromNullable(record.color
), false));
1287 if (groupsFile
.exists() && groupsFile
.length() > 0) {
1288 try (FileInputStream groupsFileStream
= new FileInputStream(groupsFile
)) {
1289 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1290 .withStream(groupsFileStream
)
1291 .withContentType("application/octet-stream")
1292 .withLength(groupsFile
.length())
1295 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
1300 Files
.delete(groupsFile
.toPath());
1301 } catch (IOException e
) {
1302 System
.err
.println("Failed to delete groups temp file “" + groupsFile
+ "”: " + e
.getMessage());
1307 private void sendContacts() throws IOException
, UntrustedIdentityException
{
1308 File contactsFile
= IOUtils
.createTempFile();
1311 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
1312 DeviceContactsOutputStream out
= new DeviceContactsOutputStream(fos
);
1313 for (ContactInfo
record : account
.getContactStore().getContacts()) {
1314 VerifiedMessage verifiedMessage
= null;
1315 ThreadInfo info
= account
.getThreadStore().getThread(record.number
);
1316 if (getIdentities().containsKey(record.number
)) {
1317 JsonIdentityKeyStore
.Identity currentIdentity
= null;
1318 for (JsonIdentityKeyStore
.Identity id
: getIdentities().get(record.number
)) {
1319 if (currentIdentity
== null || id
.getDateAdded().after(currentIdentity
.getDateAdded())) {
1320 currentIdentity
= id
;
1323 if (currentIdentity
!= null) {
1324 verifiedMessage
= new VerifiedMessage(record.number
, currentIdentity
.getIdentityKey(), currentIdentity
.getTrustLevel().toVerifiedState(), currentIdentity
.getDateAdded().getTime());
1328 byte[] profileKey
= record.profileKey
== null ?
null : Base64
.decode(record.profileKey
);
1329 // TODO store list of blocked numbers
1330 boolean blocked
= false;
1331 out
.write(new DeviceContact(record.number
, Optional
.fromNullable(record.name
),
1332 createContactAvatarAttachment(record.number
), Optional
.fromNullable(record.color
),
1333 Optional
.fromNullable(verifiedMessage
), Optional
.fromNullable(profileKey
), blocked
, Optional
.fromNullable(info
!= null ? info
.messageExpirationTime
: null)));
1336 if (account
.getProfileKey() != null) {
1337 // Send our own profile key as well
1338 out
.write(new DeviceContact(account
.getUsername(),
1339 Optional
.<String
>absent(), Optional
.<SignalServiceAttachmentStream
>absent(),
1340 Optional
.<String
>absent(), Optional
.<VerifiedMessage
>absent(),
1341 Optional
.of(account
.getProfileKey()),
1342 false, Optional
.<Integer
>absent()));
1346 if (contactsFile
.exists() && contactsFile
.length() > 0) {
1347 try (FileInputStream contactsFileStream
= new FileInputStream(contactsFile
)) {
1348 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1349 .withStream(contactsFileStream
)
1350 .withContentType("application/octet-stream")
1351 .withLength(contactsFile
.length())
1354 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
1359 Files
.delete(contactsFile
.toPath());
1360 } catch (IOException e
) {
1361 System
.err
.println("Failed to delete contacts temp file “" + contactsFile
+ "”: " + e
.getMessage());
1366 private void sendVerifiedMessage(String destination
, IdentityKey identityKey
, TrustLevel trustLevel
) throws IOException
, UntrustedIdentityException
{
1367 VerifiedMessage verifiedMessage
= new VerifiedMessage(destination
, identityKey
, trustLevel
.toVerifiedState(), System
.currentTimeMillis());
1368 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
1371 public ContactInfo
getContact(String number
) {
1372 return account
.getContactStore().getContact(number
);
1375 public GroupInfo
getGroup(byte[] groupId
) {
1376 return account
.getGroupStore().getGroup(groupId
);
1379 public Map
<String
, List
<JsonIdentityKeyStore
.Identity
>> getIdentities() {
1380 return account
.getSignalProtocolStore().getIdentities();
1383 public List
<JsonIdentityKeyStore
.Identity
> getIdentities(String number
) {
1384 return account
.getSignalProtocolStore().getIdentities(number
);
1388 * Trust this the identity with this fingerprint
1390 * @param name username of the identity
1391 * @param fingerprint Fingerprint
1393 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) {
1394 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1398 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1399 if (!Arrays
.equals(id
.getIdentityKey().serialize(), fingerprint
)) {
1403 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1405 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1406 } catch (IOException
| UntrustedIdentityException e
) {
1407 e
.printStackTrace();
1416 * Trust this the identity with this safety number
1418 * @param name username of the identity
1419 * @param safetyNumber Safety number
1421 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) {
1422 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1426 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1427 if (!safetyNumber
.equals(computeSafetyNumber(name
, id
.getIdentityKey()))) {
1431 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1433 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1434 } catch (IOException
| UntrustedIdentityException e
) {
1435 e
.printStackTrace();
1444 * Trust all keys of this identity without verification
1446 * @param name username of the identity
1448 public boolean trustIdentityAllKeys(String name
) {
1449 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1453 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1454 if (id
.getTrustLevel() == TrustLevel
.UNTRUSTED
) {
1455 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1457 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1458 } catch (IOException
| UntrustedIdentityException e
) {
1459 e
.printStackTrace();
1467 public String
computeSafetyNumber(String theirUsername
, IdentityKey theirIdentityKey
) {
1468 return Utils
.computeSafetyNumber(username
, getIdentity(), theirUsername
, theirIdentityKey
);
1471 public interface ReceiveMessageHandler
{
1473 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);