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
.push
.UnsupportedDataMessageException
;
60 import org
.whispersystems
.signalservice
.internal
.util
.Base64
;
64 import java
.nio
.file
.Files
;
65 import java
.nio
.file
.Paths
;
66 import java
.nio
.file
.StandardCopyOption
;
68 import java
.util
.concurrent
.TimeUnit
;
69 import java
.util
.concurrent
.TimeoutException
;
71 public class Manager
implements Signal
{
73 private final String settingsPath
;
74 private final String dataPath
;
75 private final String attachmentsPath
;
76 private final String avatarsPath
;
77 private final SleepTimer timer
= new UptimeSleepTimer();
79 private SignalAccount account
;
80 private String username
;
81 private SignalServiceAccountManager accountManager
;
82 private SignalServiceMessagePipe messagePipe
= null;
83 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
85 public Manager(String username
, String settingsPath
) {
86 this.username
= username
;
87 this.settingsPath
= settingsPath
;
88 this.dataPath
= this.settingsPath
+ "/data";
89 this.attachmentsPath
= this.settingsPath
+ "/attachments";
90 this.avatarsPath
= this.settingsPath
+ "/avatars";
94 public String
getUsername() {
98 private IdentityKey
getIdentity() {
99 return account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey();
102 public int getDeviceId() {
103 return account
.getDeviceId();
106 private String
getMessageCachePath() {
107 return this.dataPath
+ "/" + username
+ ".d/msg-cache";
110 private String
getMessageCachePath(String sender
) {
111 return getMessageCachePath() + "/" + sender
.replace("/", "_");
114 private File
getMessageCacheFile(String sender
, long now
, long timestamp
) throws IOException
{
115 String cachePath
= getMessageCachePath(sender
);
116 IOUtils
.createPrivateDirectories(cachePath
);
117 return new File(cachePath
+ "/" + now
+ "_" + timestamp
);
120 public boolean userHasKeys() {
121 return account
!= null && account
.getSignalProtocolStore() != null;
124 public void init() throws IOException
{
125 if (!SignalAccount
.userExists(dataPath
, username
)) {
128 account
= SignalAccount
.load(dataPath
, username
);
130 migrateLegacyConfigs();
132 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), BaseConfig
.USER_AGENT
, timer
);
134 if (account
.isRegistered() && accountManager
.getPreKeysCount() < BaseConfig
.PREKEY_MINIMUM_COUNT
) {
138 } catch (AuthorizationFailedException e
) {
139 System
.err
.println("Authorization failed, was the number registered elsewhere?");
144 private void migrateLegacyConfigs() {
145 // Copy group avatars that were previously stored in the attachments folder
146 // to the new avatar folder
147 if (JsonGroupStore
.groupsWithLegacyAvatarId
.size() > 0) {
148 for (GroupInfo g
: JsonGroupStore
.groupsWithLegacyAvatarId
) {
149 File avatarFile
= getGroupAvatarFile(g
.groupId
);
150 File attachmentFile
= getAttachmentFile(g
.getAvatarId());
151 if (!avatarFile
.exists() && attachmentFile
.exists()) {
153 IOUtils
.createPrivateDirectories(avatarsPath
);
154 Files
.copy(attachmentFile
.toPath(), avatarFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
155 } catch (Exception e
) {
160 JsonGroupStore
.groupsWithLegacyAvatarId
.clear();
163 if (account
.getProfileKey() == null) {
164 // Old config file, creating new profile key
165 account
.setProfileKey(KeyUtils
.createProfileKey());
170 private void createNewIdentity() throws IOException
{
171 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
172 int registrationId
= KeyHelper
.generateRegistrationId(false);
173 if (username
== null) {
174 account
= SignalAccount
.createTemporaryAccount(identityKey
, registrationId
);
176 byte[] profileKey
= KeyUtils
.createProfileKey();
177 account
= SignalAccount
.create(dataPath
, username
, identityKey
, registrationId
, profileKey
);
182 public boolean isRegistered() {
183 return account
!= null && account
.isRegistered();
186 public void register(boolean voiceVerification
) throws IOException
{
187 if (account
== null) {
190 account
.setPassword(KeyUtils
.createPassword());
191 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, account
.getUsername(), account
.getPassword(), BaseConfig
.USER_AGENT
, timer
);
193 if (voiceVerification
) {
194 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(), Optional
.<String
>absent(), Optional
.<String
>absent());
196 accountManager
.requestSmsVerificationCode(false, Optional
.<String
>absent(), Optional
.<String
>absent());
199 account
.setRegistered(false);
203 public void updateAccountAttributes() throws IOException
{
204 accountManager
.setAccountAttributes(account
.getSignalingKey(), account
.getSignalProtocolStore().getLocalRegistrationId(), true, account
.getRegistrationLockPin(), getSelfUnidentifiedAccessKey(), false);
207 public void unregister() throws IOException
{
208 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
209 // If this is the master device, other users can't send messages to this number anymore.
210 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
211 accountManager
.setGcmId(Optional
.<String
>absent());
214 public String
getDeviceLinkUri() throws TimeoutException
, IOException
{
215 if (account
== null) {
218 account
.setPassword(KeyUtils
.createPassword());
219 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), BaseConfig
.USER_AGENT
, timer
);
220 String uuid
= accountManager
.getNewDeviceUuid();
222 return Utils
.createDeviceLinkUri(new Utils
.DeviceLinkInfo(uuid
, getIdentity().getPublicKey()));
225 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
226 account
.setSignalingKey(KeyUtils
.createSignalingKey());
227 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(account
.getSignalProtocolStore().getIdentityKeyPair(), account
.getSignalingKey(), false, true, account
.getSignalProtocolStore().getLocalRegistrationId(), deviceName
);
229 username
= ret
.getNumber();
230 // TODO do this check before actually registering
231 if (SignalAccount
.userExists(dataPath
, username
)) {
232 throw new UserAlreadyExists(username
, SignalAccount
.getFileName(dataPath
, username
));
235 // Create new account with the synced identity
236 byte[] profileKey
= ret
.getProfileKey();
237 if (profileKey
== null) {
238 profileKey
= KeyUtils
.createProfileKey();
240 account
= SignalAccount
.createLinkedAccount(dataPath
, username
, account
.getPassword(), ret
.getDeviceId(), ret
.getIdentity(), account
.getSignalProtocolStore().getLocalRegistrationId(), account
.getSignalingKey(), profileKey
);
245 requestSyncContacts();
246 requestSyncBlocked();
247 requestSyncConfiguration();
252 public List
<DeviceInfo
> getLinkedDevices() throws IOException
{
253 List
<DeviceInfo
> devices
= accountManager
.getDevices();
254 account
.setMultiDevice(devices
.size() > 1);
259 public void removeLinkedDevices(int deviceId
) throws IOException
{
260 accountManager
.removeDevice(deviceId
);
261 List
<DeviceInfo
> devices
= accountManager
.getDevices();
262 account
.setMultiDevice(devices
.size() > 1);
266 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
267 Utils
.DeviceLinkInfo info
= Utils
.parseDeviceLinkUri(linkUri
);
269 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
272 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
273 IdentityKeyPair identityKeyPair
= account
.getSignalProtocolStore().getIdentityKeyPair();
274 String verificationCode
= accountManager
.getNewDeviceVerificationCode();
276 accountManager
.addDevice(deviceIdentifier
, deviceKey
, identityKeyPair
, Optional
.of(account
.getProfileKey()), verificationCode
);
277 account
.setMultiDevice(true);
281 private List
<PreKeyRecord
> generatePreKeys() {
282 List
<PreKeyRecord
> records
= new ArrayList
<>(BaseConfig
.PREKEY_BATCH_SIZE
);
284 final int offset
= account
.getPreKeyIdOffset();
285 for (int i
= 0; i
< BaseConfig
.PREKEY_BATCH_SIZE
; i
++) {
286 int preKeyId
= (offset
+ i
) % Medium
.MAX_VALUE
;
287 ECKeyPair keyPair
= Curve
.generateKeyPair();
288 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
293 account
.addPreKeys(records
);
299 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
301 ECKeyPair keyPair
= Curve
.generateKeyPair();
302 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
303 SignedPreKeyRecord
record = new SignedPreKeyRecord(account
.getNextSignedPreKeyId(), System
.currentTimeMillis(), keyPair
, signature
);
305 account
.addSignedPreKey(record);
309 } catch (InvalidKeyException e
) {
310 throw new AssertionError(e
);
314 public void verifyAccount(String verificationCode
, String pin
) throws IOException
{
315 verificationCode
= verificationCode
.replace("-", "");
316 account
.setSignalingKey(KeyUtils
.createSignalingKey());
317 // TODO make unrestricted unidentified access configurable
318 accountManager
.verifyAccountWithCode(verificationCode
, account
.getSignalingKey(), account
.getSignalProtocolStore().getLocalRegistrationId(), true, pin
, getSelfUnidentifiedAccessKey(), false);
320 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
321 account
.setRegistered(true);
322 account
.setRegistrationLockPin(pin
);
328 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
{
329 accountManager
.setPin(pin
);
330 if (pin
.isPresent()) {
331 account
.setRegistrationLockPin(pin
.get());
333 account
.setRegistrationLockPin(null);
338 private void refreshPreKeys() throws IOException
{
339 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
340 final IdentityKeyPair identityKeyPair
= account
.getSignalProtocolStore().getIdentityKeyPair();
341 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
343 accountManager
.setPreKeys(getIdentity(), signedPreKeyRecord
, oneTimePreKeys
);
346 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(byte[] groupId
) throws IOException
{
347 File file
= getGroupAvatarFile(groupId
);
348 if (!file
.exists()) {
349 return Optional
.absent();
352 return Optional
.of(Utils
.createAttachment(file
));
355 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(String number
) throws IOException
{
356 File file
= getContactAvatarFile(number
);
357 if (!file
.exists()) {
358 return Optional
.absent();
361 return Optional
.of(Utils
.createAttachment(file
));
364 private GroupInfo
getGroupForSending(byte[] groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
365 GroupInfo g
= account
.getGroupStore().getGroup(groupId
);
367 throw new GroupNotFoundException(groupId
);
369 for (String member
: g
.members
) {
370 if (member
.equals(this.username
)) {
374 throw new NotAGroupMemberException(groupId
, g
.name
);
377 public List
<GroupInfo
> getGroups() {
378 return account
.getGroupStore().getGroups();
382 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
384 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
385 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
386 if (attachments
!= null) {
387 messageBuilder
.withAttachments(Utils
.getSignalServiceAttachments(attachments
));
389 if (groupId
!= null) {
390 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
393 messageBuilder
.asGroupMessage(group
);
395 ThreadInfo thread
= account
.getThreadStore().getThread(Base64
.encodeBytes(groupId
));
396 if (thread
!= null) {
397 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
400 final GroupInfo g
= getGroupForSending(groupId
);
402 // Don't send group message to ourself
403 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
404 membersSend
.remove(this.username
);
405 sendMessageLegacy(messageBuilder
, membersSend
);
408 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
{
409 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
413 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
414 .asGroupMessage(group
);
416 final GroupInfo g
= getGroupForSending(groupId
);
417 g
.members
.remove(this.username
);
418 account
.getGroupStore().updateGroup(g
);
420 sendMessageLegacy(messageBuilder
, g
.members
);
423 private byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
425 if (groupId
== null) {
427 g
= new GroupInfo(KeyUtils
.createGroupId());
428 g
.members
.add(username
);
430 g
= getGroupForSending(groupId
);
437 if (members
!= null) {
438 Set
<String
> newMembers
= new HashSet
<>();
439 for (String member
: members
) {
441 member
= Utils
.canonicalizeNumber(member
, username
);
442 } catch (InvalidNumberException e
) {
443 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
444 System
.err
.println("Aborting…");
447 if (g
.members
.contains(member
)) {
450 newMembers
.add(member
);
451 g
.members
.add(member
);
453 final List
<ContactTokenDetails
> contacts
= accountManager
.getContacts(newMembers
);
454 if (contacts
.size() != newMembers
.size()) {
455 // Some of the new members are not registered on Signal
456 for (ContactTokenDetails contact
: contacts
) {
457 newMembers
.remove(contact
.getNumber());
459 System
.err
.println("Failed to add members " + Util
.join(", ", newMembers
) + " to group: Not registered on Signal");
460 System
.err
.println("Aborting…");
465 if (avatarFile
!= null) {
466 IOUtils
.createPrivateDirectories(avatarsPath
);
467 File aFile
= getGroupAvatarFile(g
.groupId
);
468 Files
.copy(Paths
.get(avatarFile
), aFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
471 account
.getGroupStore().updateGroup(g
);
473 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
475 // Don't send group message to ourself
476 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
477 membersSend
.remove(this.username
);
478 sendMessageLegacy(messageBuilder
, membersSend
);
482 private void sendUpdateGroupMessage(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
483 if (groupId
== null) {
486 GroupInfo g
= getGroupForSending(groupId
);
488 if (!g
.members
.contains(recipient
)) {
492 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
494 // Send group message only to the recipient who requested it
495 final List
<String
> membersSend
= new ArrayList
<>();
496 membersSend
.add(recipient
);
497 sendMessageLegacy(messageBuilder
, membersSend
);
500 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfo g
) {
501 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
504 .withMembers(new ArrayList
<>(g
.members
));
506 File aFile
= getGroupAvatarFile(g
.groupId
);
507 if (aFile
.exists()) {
509 group
.withAvatar(Utils
.createAttachment(aFile
));
510 } catch (IOException e
) {
511 throw new AttachmentInvalidException(aFile
.toString(), e
);
515 return SignalServiceDataMessage
.newBuilder()
516 .asGroupMessage(group
.build());
519 private void sendGroupInfoRequest(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
520 if (groupId
== null) {
524 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
)
527 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
528 .asGroupMessage(group
.build());
530 // Send group info request message to the recipient who sent us a message with this groupId
531 final List
<String
> membersSend
= new ArrayList
<>();
532 membersSend
.add(recipient
);
533 sendMessageLegacy(messageBuilder
, membersSend
);
537 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
538 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
{
539 List
<String
> recipients
= new ArrayList
<>(1);
540 recipients
.add(recipient
);
541 sendMessage(message
, attachments
, recipients
);
545 public void sendMessage(String messageText
, List
<String
> attachments
,
546 List
<String
> recipients
)
547 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
{
548 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
549 if (attachments
!= null) {
550 messageBuilder
.withAttachments(Utils
.getSignalServiceAttachments(attachments
));
552 sendMessageLegacy(messageBuilder
, recipients
);
556 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
{
557 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
558 .asEndSessionMessage();
560 sendMessageLegacy(messageBuilder
, recipients
);
564 public String
getContactName(String number
) {
565 ContactInfo contact
= account
.getContactStore().getContact(number
);
566 if (contact
== null) {
574 public void setContactName(String number
, String name
) {
575 ContactInfo contact
= account
.getContactStore().getContact(number
);
576 if (contact
== null) {
577 contact
= new ContactInfo();
578 contact
.number
= number
;
579 System
.err
.println("Add contact " + number
+ " named " + name
);
581 System
.err
.println("Updating contact " + number
+ " name " + contact
.name
+ " -> " + name
);
584 account
.getContactStore().updateContact(contact
);
589 public List
<byte[]> getGroupIds() {
590 List
<GroupInfo
> groups
= getGroups();
591 List
<byte[]> ids
= new ArrayList
<>(groups
.size());
592 for (GroupInfo group
: groups
) {
593 ids
.add(group
.groupId
);
599 public String
getGroupName(byte[] groupId
) {
600 GroupInfo group
= getGroup(groupId
);
609 public List
<String
> getGroupMembers(byte[] groupId
) {
610 GroupInfo group
= getGroup(groupId
);
612 return new ArrayList
<>();
614 return new ArrayList
<>(group
.members
);
619 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
620 if (groupId
.length
== 0) {
623 if (name
.isEmpty()) {
626 if (members
.size() == 0) {
629 if (avatar
.isEmpty()) {
632 return sendUpdateGroupMessage(groupId
, name
, members
, avatar
);
636 * Change the expiration timer for a thread (number of groupId)
638 * @param numberOrGroupId
639 * @param messageExpirationTimer
641 public void setExpirationTimer(String numberOrGroupId
, int messageExpirationTimer
) {
642 ThreadInfo thread
= account
.getThreadStore().getThread(numberOrGroupId
);
643 thread
.messageExpirationTime
= messageExpirationTimer
;
644 account
.getThreadStore().updateThread(thread
);
647 private void requestSyncGroups() throws IOException
{
648 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
).build();
649 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
651 sendSyncMessage(message
);
652 } catch (UntrustedIdentityException e
) {
657 private void requestSyncContacts() throws IOException
{
658 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
).build();
659 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
661 sendSyncMessage(message
);
662 } catch (UntrustedIdentityException e
) {
667 private void requestSyncBlocked() throws IOException
{
668 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
).build();
669 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
671 sendSyncMessage(message
);
672 } catch (UntrustedIdentityException e
) {
677 private void requestSyncConfiguration() throws IOException
{
678 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
).build();
679 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
681 sendSyncMessage(message
);
682 } catch (UntrustedIdentityException e
) {
687 private byte[] getSelfUnidentifiedAccessKey() {
688 return UnidentifiedAccess
.deriveAccessKeyFrom(account
.getProfileKey());
691 private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient
) {
696 private Optional
<UnidentifiedAccessPair
> getAccessForSync() {
698 return Optional
.absent();
701 private List
<Optional
<UnidentifiedAccessPair
>> getAccessFor(Collection
<SignalServiceAddress
> recipients
) {
702 List
<Optional
<UnidentifiedAccessPair
>> result
= new ArrayList
<>(recipients
.size());
703 for (SignalServiceAddress recipient
: recipients
) {
704 result
.add(Optional
.<UnidentifiedAccessPair
>absent());
709 private Optional
<UnidentifiedAccessPair
> getAccessFor(SignalServiceAddress recipient
) {
711 return Optional
.absent();
714 private void sendSyncMessage(SignalServiceSyncMessage message
)
715 throws IOException
, UntrustedIdentityException
{
716 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(),
717 account
.getDeviceId(), account
.getSignalProtocolStore(), BaseConfig
.USER_AGENT
, account
.isMultiDevice(), Optional
.fromNullable(messagePipe
), Optional
.fromNullable(unidentifiedMessagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
719 messageSender
.sendMessage(message
, getAccessForSync());
720 } catch (UntrustedIdentityException e
) {
721 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
727 * This method throws an EncapsulatedExceptions exception instead of returning a list of SendMessageResult.
729 private void sendMessageLegacy(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
730 throws EncapsulatedExceptions
, IOException
{
731 List
<SendMessageResult
> results
= sendMessage(messageBuilder
, recipients
);
733 List
<UntrustedIdentityException
> untrustedIdentities
= new LinkedList
<>();
734 List
<UnregisteredUserException
> unregisteredUsers
= new LinkedList
<>();
735 List
<NetworkFailureException
> networkExceptions
= new LinkedList
<>();
737 for (SendMessageResult result
: results
) {
738 if (result
.isUnregisteredFailure()) {
739 unregisteredUsers
.add(new UnregisteredUserException(result
.getAddress().getNumber(), null));
740 } else if (result
.isNetworkFailure()) {
741 networkExceptions
.add(new NetworkFailureException(result
.getAddress().getNumber(), null));
742 } else if (result
.getIdentityFailure() != null) {
743 untrustedIdentities
.add(new UntrustedIdentityException("Untrusted", result
.getAddress().getNumber(), result
.getIdentityFailure().getIdentityKey()));
746 if (!untrustedIdentities
.isEmpty() || !unregisteredUsers
.isEmpty() || !networkExceptions
.isEmpty()) {
747 throw new EncapsulatedExceptions(untrustedIdentities
, unregisteredUsers
, networkExceptions
);
751 private List
<SendMessageResult
> sendMessage(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
753 Set
<SignalServiceAddress
> recipientsTS
= Utils
.getSignalServiceAddresses(recipients
, username
);
754 if (recipientsTS
== null) {
756 return Collections
.emptyList();
759 SignalServiceDataMessage message
= null;
761 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(),
762 account
.getDeviceId(), account
.getSignalProtocolStore(), BaseConfig
.USER_AGENT
, account
.isMultiDevice(), Optional
.fromNullable(messagePipe
), Optional
.fromNullable(unidentifiedMessagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
764 message
= messageBuilder
.build();
765 if (message
.getGroupInfo().isPresent()) {
767 final boolean isRecipientUpdate
= true;
768 List
<SendMessageResult
> result
= messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), getAccessFor(recipientsTS
), isRecipientUpdate
, message
);
769 for (SendMessageResult r
: result
) {
770 if (r
.getIdentityFailure() != null) {
771 account
.getSignalProtocolStore().saveIdentity(r
.getAddress().getNumber(), r
.getIdentityFailure().getIdentityKey(), TrustLevel
.UNTRUSTED
);
775 } catch (UntrustedIdentityException e
) {
776 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
777 return Collections
.emptyList();
779 } else if (recipientsTS
.size() == 1 && recipientsTS
.contains(new SignalServiceAddress(username
))) {
780 SignalServiceAddress recipient
= new SignalServiceAddress(username
);
781 final Optional
<UnidentifiedAccessPair
> unidentifiedAccess
= getAccessFor(recipient
);
782 SentTranscriptMessage transcript
= new SentTranscriptMessage(recipient
.getNumber(),
783 message
.getTimestamp(),
785 message
.getExpiresInSeconds(),
786 Collections
.singletonMap(recipient
.getNumber(), unidentifiedAccess
.isPresent()),
788 SignalServiceSyncMessage syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
790 List
<SendMessageResult
> results
= new ArrayList
<>(recipientsTS
.size());
792 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
793 } catch (UntrustedIdentityException e
) {
794 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
795 results
.add(SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey()));
799 // Send to all individually, so sync messages are sent correctly
800 List
<SendMessageResult
> results
= new ArrayList
<>(recipientsTS
.size());
801 for (SignalServiceAddress address
: recipientsTS
) {
802 ThreadInfo thread
= account
.getThreadStore().getThread(address
.getNumber());
803 if (thread
!= null) {
804 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
806 messageBuilder
.withExpiration(0);
808 message
= messageBuilder
.build();
810 SendMessageResult result
= messageSender
.sendMessage(address
, getAccessFor(address
), message
);
812 } catch (UntrustedIdentityException e
) {
813 account
.getSignalProtocolStore().saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
814 results
.add(SendMessageResult
.identityFailure(address
, e
.getIdentityKey()));
820 if (message
!= null && message
.isEndSession()) {
821 for (SignalServiceAddress recipient
: recipientsTS
) {
822 handleEndSession(recipient
.getNumber());
829 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, ProtocolUntrustedIdentityException
, SelfSendException
, UnsupportedDataMessageException
{
830 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), account
.getSignalProtocolStore(), Utils
.getCertificateValidator());
832 return cipher
.decrypt(envelope
);
833 } catch (ProtocolUntrustedIdentityException e
) {
834 // TODO We don't get the new untrusted identity from ProtocolUntrustedIdentityException anymore ... we need to get it from somewhere else
835 // account.getSignalProtocolStore().saveIdentity(e.getSender(), e.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
840 private void handleEndSession(String source
) {
841 account
.getSignalProtocolStore().deleteAllSessions(source
);
844 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
, boolean ignoreAttachments
) {
846 if (message
.getGroupInfo().isPresent()) {
847 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
848 threadId
= Base64
.encodeBytes(groupInfo
.getGroupId());
849 GroupInfo group
= account
.getGroupStore().getGroup(groupInfo
.getGroupId());
850 switch (groupInfo
.getType()) {
853 group
= new GroupInfo(groupInfo
.getGroupId());
856 if (groupInfo
.getAvatar().isPresent()) {
857 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
858 if (avatar
.isPointer()) {
860 retrieveGroupAvatarAttachment(avatar
.asPointer(), group
.groupId
);
861 } catch (IOException
| InvalidMessageException e
) {
862 System
.err
.println("Failed to retrieve group avatar (" + avatar
.asPointer().getId() + "): " + e
.getMessage());
867 if (groupInfo
.getName().isPresent()) {
868 group
.name
= groupInfo
.getName().get();
871 if (groupInfo
.getMembers().isPresent()) {
872 group
.members
.addAll(groupInfo
.getMembers().get());
875 account
.getGroupStore().updateGroup(group
);
880 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
881 } catch (IOException
| EncapsulatedExceptions e
) {
889 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
890 } catch (IOException
| EncapsulatedExceptions e
) {
894 group
.members
.remove(source
);
895 account
.getGroupStore().updateGroup(group
);
901 sendUpdateGroupMessage(groupInfo
.getGroupId(), source
);
902 } catch (IOException
| EncapsulatedExceptions e
) {
904 } catch (NotAGroupMemberException e
) {
905 // We have left this group, so don't send a group update message
912 threadId
= destination
;
917 if (message
.isEndSession()) {
918 handleEndSession(isSync ? destination
: source
);
920 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
921 ThreadInfo thread
= account
.getThreadStore().getThread(threadId
);
922 if (thread
== null) {
923 thread
= new ThreadInfo();
924 thread
.id
= threadId
;
926 if (thread
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
927 thread
.messageExpirationTime
= message
.getExpiresInSeconds();
928 account
.getThreadStore().updateThread(thread
);
931 if (message
.getAttachments().isPresent() && !ignoreAttachments
) {
932 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
933 if (attachment
.isPointer()) {
935 retrieveAttachment(attachment
.asPointer());
936 } catch (IOException
| InvalidMessageException e
) {
937 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
942 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
943 if (source
.equals(username
)) {
944 this.account
.setProfileKey(message
.getProfileKey().get());
946 ContactInfo contact
= account
.getContactStore().getContact(source
);
947 if (contact
== null) {
948 contact
= new ContactInfo();
949 contact
.number
= source
;
951 contact
.profileKey
= Base64
.encodeBytes(message
.getProfileKey().get());
955 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
956 final File cachePath
= new File(getMessageCachePath());
957 if (!cachePath
.exists()) {
960 for (final File dir
: Objects
.requireNonNull(cachePath
.listFiles())) {
961 if (!dir
.isDirectory()) {
965 for (final File fileEntry
: Objects
.requireNonNull(dir
.listFiles())) {
966 if (!fileEntry
.isFile()) {
969 SignalServiceEnvelope envelope
;
971 envelope
= Utils
.loadEnvelope(fileEntry
);
972 if (envelope
== null) {
975 } catch (IOException e
) {
979 SignalServiceContent content
= null;
980 if (!envelope
.isReceipt()) {
982 content
= decryptMessage(envelope
);
983 } catch (Exception e
) {
986 handleMessage(envelope
, content
, ignoreAttachments
);
989 handler
.handleMessage(envelope
, content
, null);
991 Files
.delete(fileEntry
.toPath());
992 } catch (IOException e
) {
993 System
.err
.println("Failed to delete cached message file “" + fileEntry
+ "”: " + e
.getMessage());
996 // Try to delete directory if empty
1001 public void receiveMessages(long timeout
, TimeUnit unit
, boolean returnOnTimeout
, boolean ignoreAttachments
, ReceiveMessageHandler handler
) throws IOException
{
1002 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1003 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
1006 if (messagePipe
== null) {
1007 messagePipe
= messageReceiver
.createMessagePipe();
1011 SignalServiceEnvelope envelope
;
1012 SignalServiceContent content
= null;
1013 Exception exception
= null;
1014 final long now
= new Date().getTime();
1016 envelope
= messagePipe
.read(timeout
, unit
, new SignalServiceMessagePipe
.MessagePipeCallback() {
1018 public void onMessage(SignalServiceEnvelope envelope
) {
1019 // store message on disk, before acknowledging receipt to the server
1021 File cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1022 Utils
.storeEnvelope(envelope
, cacheFile
);
1023 } catch (IOException e
) {
1024 System
.err
.println("Failed to store encrypted message in disk cache, ignoring: " + e
.getMessage());
1028 } catch (TimeoutException e
) {
1029 if (returnOnTimeout
)
1032 } catch (InvalidVersionException e
) {
1033 System
.err
.println("Ignoring error: " + e
.getMessage());
1036 if (!envelope
.isReceipt()) {
1038 content
= decryptMessage(envelope
);
1039 } catch (Exception e
) {
1042 handleMessage(envelope
, content
, ignoreAttachments
);
1045 handler
.handleMessage(envelope
, content
, exception
);
1046 if (!(exception
instanceof ProtocolUntrustedIdentityException
)) {
1047 File cacheFile
= null;
1049 cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1050 Files
.delete(cacheFile
.toPath());
1051 // Try to delete directory if empty
1052 new File(getMessageCachePath()).delete();
1053 } catch (IOException e
) {
1054 System
.err
.println("Failed to delete cached message file “" + cacheFile
+ "”: " + e
.getMessage());
1059 if (messagePipe
!= null) {
1060 messagePipe
.shutdown();
1066 private void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
) {
1067 if (content
!= null) {
1068 if (content
.getDataMessage().isPresent()) {
1069 SignalServiceDataMessage message
= content
.getDataMessage().get();
1070 handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
, ignoreAttachments
);
1072 if (content
.getSyncMessage().isPresent()) {
1073 account
.setMultiDevice(true);
1074 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
1075 if (syncMessage
.getSent().isPresent()) {
1076 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
1077 handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get(), ignoreAttachments
);
1079 if (syncMessage
.getRequest().isPresent()) {
1080 RequestMessage rm
= syncMessage
.getRequest().get();
1081 if (rm
.isContactsRequest()) {
1084 } catch (UntrustedIdentityException
| IOException e
) {
1085 e
.printStackTrace();
1088 if (rm
.isGroupsRequest()) {
1091 } catch (UntrustedIdentityException
| IOException e
) {
1092 e
.printStackTrace();
1095 // TODO Handle rm.isBlockedListRequest(); rm.isConfigurationRequest();
1097 if (syncMessage
.getGroups().isPresent()) {
1098 File tmpFile
= null;
1100 tmpFile
= IOUtils
.createTempFile();
1101 try (InputStream attachmentAsStream
= retrieveAttachmentAsStream(syncMessage
.getGroups().get().asPointer(), tmpFile
)) {
1102 DeviceGroupsInputStream s
= new DeviceGroupsInputStream(attachmentAsStream
);
1104 while ((g
= s
.read()) != null) {
1105 GroupInfo syncGroup
= account
.getGroupStore().getGroup(g
.getId());
1106 if (syncGroup
== null) {
1107 syncGroup
= new GroupInfo(g
.getId());
1109 if (g
.getName().isPresent()) {
1110 syncGroup
.name
= g
.getName().get();
1112 syncGroup
.members
.addAll(g
.getMembers());
1113 syncGroup
.active
= g
.isActive();
1114 if (g
.getColor().isPresent()) {
1115 syncGroup
.color
= g
.getColor().get();
1118 if (g
.getAvatar().isPresent()) {
1119 retrieveGroupAvatarAttachment(g
.getAvatar().get(), syncGroup
.groupId
);
1121 account
.getGroupStore().updateGroup(syncGroup
);
1124 } catch (Exception e
) {
1125 e
.printStackTrace();
1127 if (tmpFile
!= null) {
1129 Files
.delete(tmpFile
.toPath());
1130 } catch (IOException e
) {
1131 System
.err
.println("Failed to delete received groups temp file “" + tmpFile
+ "”: " + e
.getMessage());
1136 if (syncMessage
.getBlockedList().isPresent()) {
1137 // TODO store list of blocked numbers
1139 if (syncMessage
.getContacts().isPresent()) {
1140 File tmpFile
= null;
1142 tmpFile
= IOUtils
.createTempFile();
1143 final ContactsMessage contactsMessage
= syncMessage
.getContacts().get();
1144 try (InputStream attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream().asPointer(), tmpFile
)) {
1145 DeviceContactsInputStream s
= new DeviceContactsInputStream(attachmentAsStream
);
1146 if (contactsMessage
.isComplete()) {
1147 account
.getContactStore().clear();
1150 while ((c
= s
.read()) != null) {
1151 if (c
.getNumber().equals(account
.getUsername()) && c
.getProfileKey().isPresent()) {
1152 account
.setProfileKey(c
.getProfileKey().get());
1154 ContactInfo contact
= account
.getContactStore().getContact(c
.getNumber());
1155 if (contact
== null) {
1156 contact
= new ContactInfo();
1157 contact
.number
= c
.getNumber();
1159 if (c
.getName().isPresent()) {
1160 contact
.name
= c
.getName().get();
1162 if (c
.getColor().isPresent()) {
1163 contact
.color
= c
.getColor().get();
1165 if (c
.getProfileKey().isPresent()) {
1166 contact
.profileKey
= Base64
.encodeBytes(c
.getProfileKey().get());
1168 if (c
.getVerified().isPresent()) {
1169 final VerifiedMessage verifiedMessage
= c
.getVerified().get();
1170 account
.getSignalProtocolStore().saveIdentity(verifiedMessage
.getDestination(), verifiedMessage
.getIdentityKey(), TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
1172 if (c
.getExpirationTimer().isPresent()) {
1173 ThreadInfo thread
= account
.getThreadStore().getThread(c
.getNumber());
1174 if (thread
== null) {
1175 thread
= new ThreadInfo();
1176 thread
.id
= c
.getNumber();
1178 thread
.messageExpirationTime
= c
.getExpirationTimer().get();
1179 account
.getThreadStore().updateThread(thread
);
1181 if (c
.isBlocked()) {
1182 // TODO store list of blocked numbers
1184 account
.getContactStore().updateContact(contact
);
1186 if (c
.getAvatar().isPresent()) {
1187 retrieveContactAvatarAttachment(c
.getAvatar().get(), contact
.number
);
1191 } catch (Exception e
) {
1192 e
.printStackTrace();
1194 if (tmpFile
!= null) {
1196 Files
.delete(tmpFile
.toPath());
1197 } catch (IOException e
) {
1198 System
.err
.println("Failed to delete received contacts temp file “" + tmpFile
+ "”: " + e
.getMessage());
1203 if (syncMessage
.getVerified().isPresent()) {
1204 final VerifiedMessage verifiedMessage
= syncMessage
.getVerified().get();
1205 account
.getSignalProtocolStore().saveIdentity(verifiedMessage
.getDestination(), verifiedMessage
.getIdentityKey(), TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
1207 if (syncMessage
.getConfiguration().isPresent()) {
1214 private File
getContactAvatarFile(String number
) {
1215 return new File(avatarsPath
, "contact-" + number
);
1218 private File
retrieveContactAvatarAttachment(SignalServiceAttachment attachment
, String number
) throws IOException
, InvalidMessageException
{
1219 IOUtils
.createPrivateDirectories(avatarsPath
);
1220 if (attachment
.isPointer()) {
1221 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1222 return retrieveAttachment(pointer
, getContactAvatarFile(number
), false);
1224 SignalServiceAttachmentStream stream
= attachment
.asStream();
1225 return Utils
.retrieveAttachment(stream
, getContactAvatarFile(number
));
1229 private File
getGroupAvatarFile(byte[] groupId
) {
1230 return new File(avatarsPath
, "group-" + Base64
.encodeBytes(groupId
).replace("/", "_"));
1233 private File
retrieveGroupAvatarAttachment(SignalServiceAttachment attachment
, byte[] groupId
) throws IOException
, InvalidMessageException
{
1234 IOUtils
.createPrivateDirectories(avatarsPath
);
1235 if (attachment
.isPointer()) {
1236 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1237 return retrieveAttachment(pointer
, getGroupAvatarFile(groupId
), false);
1239 SignalServiceAttachmentStream stream
= attachment
.asStream();
1240 return Utils
.retrieveAttachment(stream
, getGroupAvatarFile(groupId
));
1244 public File
getAttachmentFile(long attachmentId
) {
1245 return new File(attachmentsPath
, attachmentId
+ "");
1248 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
1249 IOUtils
.createPrivateDirectories(attachmentsPath
);
1250 return retrieveAttachment(pointer
, getAttachmentFile(pointer
.getId()), true);
1253 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
, File outputFile
, boolean storePreview
) throws IOException
, InvalidMessageException
{
1254 if (storePreview
&& pointer
.getPreview().isPresent()) {
1255 File previewFile
= new File(outputFile
+ ".preview");
1256 try (OutputStream output
= new FileOutputStream(previewFile
)) {
1257 byte[] preview
= pointer
.getPreview().get();
1258 output
.write(preview
, 0, preview
.length
);
1259 } catch (FileNotFoundException e
) {
1260 e
.printStackTrace();
1265 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
1267 File tmpFile
= IOUtils
.createTempFile();
1268 try (InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
, BaseConfig
.MAX_ATTACHMENT_SIZE
)) {
1269 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1270 byte[] buffer
= new byte[4096];
1273 while ((read
= input
.read(buffer
)) != -1) {
1274 output
.write(buffer
, 0, read
);
1276 } catch (FileNotFoundException e
) {
1277 e
.printStackTrace();
1282 Files
.delete(tmpFile
.toPath());
1283 } catch (IOException e
) {
1284 System
.err
.println("Failed to delete received attachment temp file “" + tmpFile
+ "”: " + e
.getMessage());
1290 private InputStream
retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer
, File tmpFile
) throws IOException
, InvalidMessageException
{
1291 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
1292 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, BaseConfig
.MAX_ATTACHMENT_SIZE
);
1296 public boolean isRemote() {
1300 private void sendGroups() throws IOException
, UntrustedIdentityException
{
1301 File groupsFile
= IOUtils
.createTempFile();
1304 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
1305 DeviceGroupsOutputStream out
= new DeviceGroupsOutputStream(fos
);
1306 for (GroupInfo
record : account
.getGroupStore().getGroups()) {
1307 ThreadInfo info
= account
.getThreadStore().getThread(Base64
.encodeBytes(record.groupId
));
1308 out
.write(new DeviceGroup(record.groupId
, Optional
.fromNullable(record.name
),
1309 new ArrayList
<>(record.members
), createGroupAvatarAttachment(record.groupId
),
1310 record.active
, Optional
.fromNullable(info
!= null ? info
.messageExpirationTime
: null),
1311 Optional
.fromNullable(record.color
), false));
1315 if (groupsFile
.exists() && groupsFile
.length() > 0) {
1316 try (FileInputStream groupsFileStream
= new FileInputStream(groupsFile
)) {
1317 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1318 .withStream(groupsFileStream
)
1319 .withContentType("application/octet-stream")
1320 .withLength(groupsFile
.length())
1323 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
1328 Files
.delete(groupsFile
.toPath());
1329 } catch (IOException e
) {
1330 System
.err
.println("Failed to delete groups temp file “" + groupsFile
+ "”: " + e
.getMessage());
1335 private void sendContacts() throws IOException
, UntrustedIdentityException
{
1336 File contactsFile
= IOUtils
.createTempFile();
1339 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
1340 DeviceContactsOutputStream out
= new DeviceContactsOutputStream(fos
);
1341 for (ContactInfo
record : account
.getContactStore().getContacts()) {
1342 VerifiedMessage verifiedMessage
= null;
1343 ThreadInfo info
= account
.getThreadStore().getThread(record.number
);
1344 if (getIdentities().containsKey(record.number
)) {
1345 JsonIdentityKeyStore
.Identity currentIdentity
= null;
1346 for (JsonIdentityKeyStore
.Identity id
: getIdentities().get(record.number
)) {
1347 if (currentIdentity
== null || id
.getDateAdded().after(currentIdentity
.getDateAdded())) {
1348 currentIdentity
= id
;
1351 if (currentIdentity
!= null) {
1352 verifiedMessage
= new VerifiedMessage(record.number
, currentIdentity
.getIdentityKey(), currentIdentity
.getTrustLevel().toVerifiedState(), currentIdentity
.getDateAdded().getTime());
1356 byte[] profileKey
= record.profileKey
== null ?
null : Base64
.decode(record.profileKey
);
1357 // TODO store list of blocked numbers
1358 boolean blocked
= false;
1359 out
.write(new DeviceContact(record.number
, Optional
.fromNullable(record.name
),
1360 createContactAvatarAttachment(record.number
), Optional
.fromNullable(record.color
),
1361 Optional
.fromNullable(verifiedMessage
), Optional
.fromNullable(profileKey
), blocked
, Optional
.fromNullable(info
!= null ? info
.messageExpirationTime
: null)));
1364 if (account
.getProfileKey() != null) {
1365 // Send our own profile key as well
1366 out
.write(new DeviceContact(account
.getUsername(),
1367 Optional
.<String
>absent(), Optional
.<SignalServiceAttachmentStream
>absent(),
1368 Optional
.<String
>absent(), Optional
.<VerifiedMessage
>absent(),
1369 Optional
.of(account
.getProfileKey()),
1370 false, Optional
.<Integer
>absent()));
1374 if (contactsFile
.exists() && contactsFile
.length() > 0) {
1375 try (FileInputStream contactsFileStream
= new FileInputStream(contactsFile
)) {
1376 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1377 .withStream(contactsFileStream
)
1378 .withContentType("application/octet-stream")
1379 .withLength(contactsFile
.length())
1382 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
1387 Files
.delete(contactsFile
.toPath());
1388 } catch (IOException e
) {
1389 System
.err
.println("Failed to delete contacts temp file “" + contactsFile
+ "”: " + e
.getMessage());
1394 private void sendVerifiedMessage(String destination
, IdentityKey identityKey
, TrustLevel trustLevel
) throws IOException
, UntrustedIdentityException
{
1395 VerifiedMessage verifiedMessage
= new VerifiedMessage(destination
, identityKey
, trustLevel
.toVerifiedState(), System
.currentTimeMillis());
1396 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
1399 public ContactInfo
getContact(String number
) {
1400 return account
.getContactStore().getContact(number
);
1403 public GroupInfo
getGroup(byte[] groupId
) {
1404 return account
.getGroupStore().getGroup(groupId
);
1407 public Map
<String
, List
<JsonIdentityKeyStore
.Identity
>> getIdentities() {
1408 return account
.getSignalProtocolStore().getIdentities();
1411 public List
<JsonIdentityKeyStore
.Identity
> getIdentities(String number
) {
1412 return account
.getSignalProtocolStore().getIdentities(number
);
1416 * Trust this the identity with this fingerprint
1418 * @param name username of the identity
1419 * @param fingerprint Fingerprint
1421 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) {
1422 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1426 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1427 if (!Arrays
.equals(id
.getIdentityKey().serialize(), fingerprint
)) {
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 this the identity with this safety number
1446 * @param name username of the identity
1447 * @param safetyNumber Safety number
1449 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) {
1450 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1454 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1455 if (!safetyNumber
.equals(computeSafetyNumber(name
, id
.getIdentityKey()))) {
1459 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1461 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1462 } catch (IOException
| UntrustedIdentityException e
) {
1463 e
.printStackTrace();
1472 * Trust all keys of this identity without verification
1474 * @param name username of the identity
1476 public boolean trustIdentityAllKeys(String name
) {
1477 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1481 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1482 if (id
.getTrustLevel() == TrustLevel
.UNTRUSTED
) {
1483 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1485 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1486 } catch (IOException
| UntrustedIdentityException e
) {
1487 e
.printStackTrace();
1495 public String
computeSafetyNumber(String theirUsername
, IdentityKey theirIdentityKey
) {
1496 return Utils
.computeSafetyNumber(username
, getIdentity(), theirUsername
, theirIdentityKey
);
1499 public interface ReceiveMessageHandler
{
1501 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);