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
.InvalidMetadataMessageException
;
30 import org
.signal
.libsignal
.metadata
.InvalidMetadataVersionException
;
31 import org
.signal
.libsignal
.metadata
.ProtocolDuplicateMessageException
;
32 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyException
;
33 import org
.signal
.libsignal
.metadata
.ProtocolInvalidKeyIdException
;
34 import org
.signal
.libsignal
.metadata
.ProtocolInvalidMessageException
;
35 import org
.signal
.libsignal
.metadata
.ProtocolInvalidVersionException
;
36 import org
.signal
.libsignal
.metadata
.ProtocolLegacyMessageException
;
37 import org
.signal
.libsignal
.metadata
.ProtocolNoSessionException
;
38 import org
.signal
.libsignal
.metadata
.ProtocolUntrustedIdentityException
;
39 import org
.signal
.libsignal
.metadata
.SelfSendException
;
40 import org
.whispersystems
.libsignal
.IdentityKey
;
41 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
42 import org
.whispersystems
.libsignal
.InvalidKeyException
;
43 import org
.whispersystems
.libsignal
.InvalidMessageException
;
44 import org
.whispersystems
.libsignal
.InvalidVersionException
;
45 import org
.whispersystems
.libsignal
.ecc
.Curve
;
46 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
47 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
48 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
49 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
50 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
51 import org
.whispersystems
.libsignal
.util
.Medium
;
52 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
53 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
54 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
55 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
56 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
57 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
58 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccess
;
59 import org
.whispersystems
.signalservice
.api
.crypto
.UnidentifiedAccessPair
;
60 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
61 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
62 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
63 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
64 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
65 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceContent
;
66 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceDataMessage
;
67 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
68 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceGroup
;
69 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
70 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
71 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
72 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
73 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
74 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
75 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
76 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceInfo
;
77 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
78 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SentTranscriptMessage
;
79 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
80 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
81 import org
.whispersystems
.signalservice
.api
.push
.ContactTokenDetails
;
82 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
83 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
84 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
85 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.NetworkFailureException
;
86 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.UnregisteredUserException
;
87 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
88 import org
.whispersystems
.signalservice
.api
.util
.SleepTimer
;
89 import org
.whispersystems
.signalservice
.api
.util
.StreamDetails
;
90 import org
.whispersystems
.signalservice
.api
.util
.UptimeSleepTimer
;
91 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
92 import org
.whispersystems
.signalservice
.internal
.push
.UnsupportedDataMessageException
;
93 import org
.whispersystems
.util
.Base64
;
96 import java
.io
.FileInputStream
;
97 import java
.io
.FileNotFoundException
;
98 import java
.io
.FileOutputStream
;
99 import java
.io
.IOException
;
100 import java
.io
.InputStream
;
101 import java
.io
.OutputStream
;
103 import java
.nio
.file
.Files
;
104 import java
.nio
.file
.Paths
;
105 import java
.nio
.file
.StandardCopyOption
;
106 import java
.util
.ArrayList
;
107 import java
.util
.Arrays
;
108 import java
.util
.Collection
;
109 import java
.util
.Collections
;
110 import java
.util
.Date
;
111 import java
.util
.HashSet
;
112 import java
.util
.LinkedList
;
113 import java
.util
.List
;
114 import java
.util
.Locale
;
115 import java
.util
.Map
;
116 import java
.util
.Objects
;
117 import java
.util
.Set
;
118 import java
.util
.concurrent
.TimeUnit
;
119 import java
.util
.concurrent
.TimeoutException
;
121 public class Manager
implements Signal
{
123 private final String settingsPath
;
124 private final String dataPath
;
125 private final String attachmentsPath
;
126 private final String avatarsPath
;
127 private final SleepTimer timer
= new UptimeSleepTimer();
129 private SignalAccount account
;
130 private String username
;
131 private SignalServiceAccountManager accountManager
;
132 private SignalServiceMessagePipe messagePipe
= null;
133 private SignalServiceMessagePipe unidentifiedMessagePipe
= null;
135 public Manager(String username
, String settingsPath
) {
136 this.username
= username
;
137 this.settingsPath
= settingsPath
;
138 this.dataPath
= this.settingsPath
+ "/data";
139 this.attachmentsPath
= this.settingsPath
+ "/attachments";
140 this.avatarsPath
= this.settingsPath
+ "/avatars";
144 public String
getUsername() {
148 private IdentityKey
getIdentity() {
149 return account
.getSignalProtocolStore().getIdentityKeyPair().getPublicKey();
152 public int getDeviceId() {
153 return account
.getDeviceId();
156 private String
getMessageCachePath() {
157 return this.dataPath
+ "/" + username
+ ".d/msg-cache";
160 private String
getMessageCachePath(String sender
) {
161 return getMessageCachePath() + "/" + sender
.replace("/", "_");
164 private File
getMessageCacheFile(String sender
, long now
, long timestamp
) throws IOException
{
165 String cachePath
= getMessageCachePath(sender
);
166 IOUtils
.createPrivateDirectories(cachePath
);
167 return new File(cachePath
+ "/" + now
+ "_" + timestamp
);
170 public boolean userHasKeys() {
171 return account
!= null && account
.getSignalProtocolStore() != null;
174 public void init() throws IOException
{
175 if (!SignalAccount
.userExists(dataPath
, username
)) {
178 account
= SignalAccount
.load(dataPath
, username
);
180 migrateLegacyConfigs();
182 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, null, username
, account
.getPassword(), account
.getDeviceId(), BaseConfig
.USER_AGENT
, timer
);
184 if (account
.isRegistered() && accountManager
.getPreKeysCount() < BaseConfig
.PREKEY_MINIMUM_COUNT
) {
188 } catch (AuthorizationFailedException e
) {
189 System
.err
.println("Authorization failed, was the number registered elsewhere?");
194 private void migrateLegacyConfigs() {
195 // Copy group avatars that were previously stored in the attachments folder
196 // to the new avatar folder
197 if (JsonGroupStore
.groupsWithLegacyAvatarId
.size() > 0) {
198 for (GroupInfo g
: JsonGroupStore
.groupsWithLegacyAvatarId
) {
199 File avatarFile
= getGroupAvatarFile(g
.groupId
);
200 File attachmentFile
= getAttachmentFile(g
.getAvatarId());
201 if (!avatarFile
.exists() && attachmentFile
.exists()) {
203 IOUtils
.createPrivateDirectories(avatarsPath
);
204 Files
.copy(attachmentFile
.toPath(), avatarFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
205 } catch (Exception e
) {
210 JsonGroupStore
.groupsWithLegacyAvatarId
.clear();
213 if (account
.getProfileKey() == null) {
214 // Old config file, creating new profile key
215 account
.setProfileKey(KeyUtils
.createProfileKey());
220 private void createNewIdentity() throws IOException
{
221 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
222 int registrationId
= KeyHelper
.generateRegistrationId(false);
223 if (username
== null) {
224 account
= SignalAccount
.createTemporaryAccount(identityKey
, registrationId
);
226 byte[] profileKey
= KeyUtils
.createProfileKey();
227 account
= SignalAccount
.create(dataPath
, username
, identityKey
, registrationId
, profileKey
);
232 public boolean isRegistered() {
233 return account
!= null && account
.isRegistered();
236 public void register(boolean voiceVerification
) throws IOException
{
237 if (account
== null) {
240 account
.setPassword(KeyUtils
.createPassword());
241 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, null, account
.getUsername(), account
.getPassword(), BaseConfig
.USER_AGENT
, timer
);
243 if (voiceVerification
) {
244 accountManager
.requestVoiceVerificationCode(Locale
.getDefault(), Optional
.<String
>absent(), Optional
.<String
>absent());
246 accountManager
.requestSmsVerificationCode(false, Optional
.<String
>absent(), Optional
.<String
>absent());
249 account
.setRegistered(false);
253 public void updateAccountAttributes() throws IOException
{
254 accountManager
.setAccountAttributes(account
.getSignalingKey(), account
.getSignalProtocolStore().getLocalRegistrationId(), true, account
.getRegistrationLockPin(), getSelfUnidentifiedAccessKey(), false);
257 public void setProfileName(String name
) throws IOException
{
258 accountManager
.setProfileName(account
.getProfileKey(), name
);
261 public void setProfileAvatar(File avatar
) throws IOException
{
262 final StreamDetails streamDetails
= Utils
.createStreamDetailsFromFile(avatar
);
263 accountManager
.setProfileAvatar(account
.getProfileKey(), streamDetails
);
264 streamDetails
.getStream().close();
267 public void removeProfileAvatar() throws IOException
{
268 accountManager
.setProfileAvatar(account
.getProfileKey(), null);
271 public void unregister() throws IOException
{
272 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
273 // If this is the master device, other users can't send messages to this number anymore.
274 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
275 accountManager
.setGcmId(Optional
.<String
>absent());
277 account
.setRegistered(false);
281 public String
getDeviceLinkUri() throws TimeoutException
, IOException
{
282 if (account
== null) {
285 account
.setPassword(KeyUtils
.createPassword());
286 accountManager
= new SignalServiceAccountManager(BaseConfig
.serviceConfiguration
, null, username
, account
.getPassword(), BaseConfig
.USER_AGENT
, timer
);
287 String uuid
= accountManager
.getNewDeviceUuid();
289 return Utils
.createDeviceLinkUri(new Utils
.DeviceLinkInfo(uuid
, getIdentity().getPublicKey()));
292 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
293 account
.setSignalingKey(KeyUtils
.createSignalingKey());
294 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(account
.getSignalProtocolStore().getIdentityKeyPair(), account
.getSignalingKey(), false, true, account
.getSignalProtocolStore().getLocalRegistrationId(), deviceName
);
296 username
= ret
.getNumber();
297 // TODO do this check before actually registering
298 if (SignalAccount
.userExists(dataPath
, username
)) {
299 throw new UserAlreadyExists(username
, SignalAccount
.getFileName(dataPath
, username
));
302 // Create new account with the synced identity
303 byte[] profileKey
= ret
.getProfileKey();
304 if (profileKey
== null) {
305 profileKey
= KeyUtils
.createProfileKey();
307 account
= SignalAccount
.createLinkedAccount(dataPath
, username
, account
.getPassword(), ret
.getDeviceId(), ret
.getIdentity(), account
.getSignalProtocolStore().getLocalRegistrationId(), account
.getSignalingKey(), profileKey
);
312 requestSyncContacts();
313 requestSyncBlocked();
314 requestSyncConfiguration();
319 public List
<DeviceInfo
> getLinkedDevices() throws IOException
{
320 List
<DeviceInfo
> devices
= accountManager
.getDevices();
321 account
.setMultiDevice(devices
.size() > 1);
326 public void removeLinkedDevices(int deviceId
) throws IOException
{
327 accountManager
.removeDevice(deviceId
);
328 List
<DeviceInfo
> devices
= accountManager
.getDevices();
329 account
.setMultiDevice(devices
.size() > 1);
333 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
334 Utils
.DeviceLinkInfo info
= Utils
.parseDeviceLinkUri(linkUri
);
336 addDevice(info
.deviceIdentifier
, info
.deviceKey
);
339 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
340 IdentityKeyPair identityKeyPair
= account
.getSignalProtocolStore().getIdentityKeyPair();
341 String verificationCode
= accountManager
.getNewDeviceVerificationCode();
343 accountManager
.addDevice(deviceIdentifier
, deviceKey
, identityKeyPair
, Optional
.of(account
.getProfileKey()), verificationCode
);
344 account
.setMultiDevice(true);
348 private List
<PreKeyRecord
> generatePreKeys() {
349 List
<PreKeyRecord
> records
= new ArrayList
<>(BaseConfig
.PREKEY_BATCH_SIZE
);
351 final int offset
= account
.getPreKeyIdOffset();
352 for (int i
= 0; i
< BaseConfig
.PREKEY_BATCH_SIZE
; i
++) {
353 int preKeyId
= (offset
+ i
) % Medium
.MAX_VALUE
;
354 ECKeyPair keyPair
= Curve
.generateKeyPair();
355 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
360 account
.addPreKeys(records
);
366 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
368 ECKeyPair keyPair
= Curve
.generateKeyPair();
369 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
370 SignedPreKeyRecord
record = new SignedPreKeyRecord(account
.getNextSignedPreKeyId(), System
.currentTimeMillis(), keyPair
, signature
);
372 account
.addSignedPreKey(record);
376 } catch (InvalidKeyException e
) {
377 throw new AssertionError(e
);
381 public void verifyAccount(String verificationCode
, String pin
) throws IOException
{
382 verificationCode
= verificationCode
.replace("-", "");
383 account
.setSignalingKey(KeyUtils
.createSignalingKey());
384 // TODO make unrestricted unidentified access configurable
385 accountManager
.verifyAccountWithCode(verificationCode
, account
.getSignalingKey(), account
.getSignalProtocolStore().getLocalRegistrationId(), true, pin
, getSelfUnidentifiedAccessKey(), false);
387 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
388 account
.setRegistered(true);
389 account
.setRegistrationLockPin(pin
);
395 public void setRegistrationLockPin(Optional
<String
> pin
) throws IOException
{
396 accountManager
.setPin(pin
);
397 if (pin
.isPresent()) {
398 account
.setRegistrationLockPin(pin
.get());
400 account
.setRegistrationLockPin(null);
405 private void refreshPreKeys() throws IOException
{
406 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
407 final IdentityKeyPair identityKeyPair
= account
.getSignalProtocolStore().getIdentityKeyPair();
408 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(identityKeyPair
);
410 accountManager
.setPreKeys(getIdentity(), signedPreKeyRecord
, oneTimePreKeys
);
413 private SignalServiceMessageReceiver
getMessageReceiver() {
414 return new SignalServiceMessageReceiver(BaseConfig
.serviceConfiguration
, null, username
, account
.getPassword(), account
.getDeviceId(), account
.getSignalingKey(), BaseConfig
.USER_AGENT
, null, timer
);
417 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(byte[] groupId
) throws IOException
{
418 File file
= getGroupAvatarFile(groupId
);
419 if (!file
.exists()) {
420 return Optional
.absent();
423 return Optional
.of(Utils
.createAttachment(file
));
426 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(String number
) throws IOException
{
427 File file
= getContactAvatarFile(number
);
428 if (!file
.exists()) {
429 return Optional
.absent();
432 return Optional
.of(Utils
.createAttachment(file
));
435 private GroupInfo
getGroupForSending(byte[] groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
436 GroupInfo g
= account
.getGroupStore().getGroup(groupId
);
438 throw new GroupNotFoundException(groupId
);
440 for (String member
: g
.members
) {
441 if (member
.equals(this.username
)) {
445 throw new NotAGroupMemberException(groupId
, g
.name
);
448 public List
<GroupInfo
> getGroups() {
449 return account
.getGroupStore().getGroups();
453 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
455 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
456 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
457 if (attachments
!= null) {
458 messageBuilder
.withAttachments(Utils
.getSignalServiceAttachments(attachments
));
460 if (groupId
!= null) {
461 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
464 messageBuilder
.asGroupMessage(group
);
466 ThreadInfo thread
= account
.getThreadStore().getThread(Base64
.encodeBytes(groupId
));
467 if (thread
!= null) {
468 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
471 final GroupInfo g
= getGroupForSending(groupId
);
473 // Don't send group message to ourself
474 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
475 membersSend
.remove(this.username
);
476 sendMessageLegacy(messageBuilder
, membersSend
);
479 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
{
480 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
484 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
485 .asGroupMessage(group
);
487 final GroupInfo g
= getGroupForSending(groupId
);
488 g
.members
.remove(this.username
);
489 account
.getGroupStore().updateGroup(g
);
491 sendMessageLegacy(messageBuilder
, g
.members
);
494 private byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
496 if (groupId
== null) {
498 g
= new GroupInfo(KeyUtils
.createGroupId());
499 g
.members
.add(username
);
501 g
= getGroupForSending(groupId
);
508 if (members
!= null) {
509 Set
<String
> newMembers
= new HashSet
<>();
510 for (String member
: members
) {
512 member
= Utils
.canonicalizeNumber(member
, username
);
513 } catch (InvalidNumberException e
) {
514 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
515 System
.err
.println("Aborting…");
518 if (g
.members
.contains(member
)) {
521 newMembers
.add(member
);
522 g
.members
.add(member
);
524 final List
<ContactTokenDetails
> contacts
= accountManager
.getContacts(newMembers
);
525 if (contacts
.size() != newMembers
.size()) {
526 // Some of the new members are not registered on Signal
527 for (ContactTokenDetails contact
: contacts
) {
528 newMembers
.remove(contact
.getNumber());
530 System
.err
.println("Failed to add members " + Util
.join(", ", newMembers
) + " to group: Not registered on Signal");
531 System
.err
.println("Aborting…");
536 if (avatarFile
!= null) {
537 IOUtils
.createPrivateDirectories(avatarsPath
);
538 File aFile
= getGroupAvatarFile(g
.groupId
);
539 Files
.copy(Paths
.get(avatarFile
), aFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
542 account
.getGroupStore().updateGroup(g
);
544 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
546 // Don't send group message to ourself
547 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
548 membersSend
.remove(this.username
);
549 sendMessageLegacy(messageBuilder
, membersSend
);
553 private void sendUpdateGroupMessage(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
554 if (groupId
== null) {
557 GroupInfo g
= getGroupForSending(groupId
);
559 if (!g
.members
.contains(recipient
)) {
563 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
565 // Send group message only to the recipient who requested it
566 final List
<String
> membersSend
= new ArrayList
<>();
567 membersSend
.add(recipient
);
568 sendMessageLegacy(messageBuilder
, membersSend
);
571 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfo g
) {
572 ArrayList
<SignalServiceAddress
> members
= new ArrayList
<>(g
.members
.size());
573 for (String member
: g
.members
) {
574 members
.add(new SignalServiceAddress(null, member
));
577 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
580 .withMembers(members
);
582 File aFile
= getGroupAvatarFile(g
.groupId
);
583 if (aFile
.exists()) {
585 group
.withAvatar(Utils
.createAttachment(aFile
));
586 } catch (IOException e
) {
587 throw new AttachmentInvalidException(aFile
.toString(), e
);
591 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
592 .asGroupMessage(group
.build());
594 ThreadInfo thread
= account
.getThreadStore().getThread(Base64
.encodeBytes(g
.groupId
));
595 if (thread
!= null) {
596 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
599 return messageBuilder
;
602 private void sendGroupInfoRequest(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
603 if (groupId
== null) {
607 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
)
610 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
611 .asGroupMessage(group
.build());
613 ThreadInfo thread
= account
.getThreadStore().getThread(Base64
.encodeBytes(groupId
));
614 if (thread
!= null) {
615 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
618 // Send group info request message to the recipient who sent us a message with this groupId
619 final List
<String
> membersSend
= new ArrayList
<>();
620 membersSend
.add(recipient
);
621 sendMessageLegacy(messageBuilder
, membersSend
);
625 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
626 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
{
627 List
<String
> recipients
= new ArrayList
<>(1);
628 recipients
.add(recipient
);
629 sendMessage(message
, attachments
, recipients
);
633 public void sendMessage(String messageText
, List
<String
> attachments
,
634 List
<String
> recipients
)
635 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
{
636 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
637 if (attachments
!= null) {
638 messageBuilder
.withAttachments(Utils
.getSignalServiceAttachments(attachments
));
640 messageBuilder
.withProfileKey(account
.getProfileKey());
641 sendMessageLegacy(messageBuilder
, recipients
);
645 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
{
646 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
647 .asEndSessionMessage();
649 sendMessageLegacy(messageBuilder
, recipients
);
653 public String
getContactName(String number
) {
654 ContactInfo contact
= account
.getContactStore().getContact(number
);
655 if (contact
== null) {
663 public void setContactName(String number
, String name
) {
664 ContactInfo contact
= account
.getContactStore().getContact(number
);
665 if (contact
== null) {
666 contact
= new ContactInfo();
667 contact
.number
= number
;
668 System
.err
.println("Add contact " + number
+ " named " + name
);
670 System
.err
.println("Updating contact " + number
+ " name " + contact
.name
+ " -> " + name
);
673 account
.getContactStore().updateContact(contact
);
678 public List
<byte[]> getGroupIds() {
679 List
<GroupInfo
> groups
= getGroups();
680 List
<byte[]> ids
= new ArrayList
<>(groups
.size());
681 for (GroupInfo group
: groups
) {
682 ids
.add(group
.groupId
);
688 public String
getGroupName(byte[] groupId
) {
689 GroupInfo group
= getGroup(groupId
);
698 public List
<String
> getGroupMembers(byte[] groupId
) {
699 GroupInfo group
= getGroup(groupId
);
701 return new ArrayList
<>();
703 return new ArrayList
<>(group
.members
);
708 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
709 if (groupId
.length
== 0) {
712 if (name
.isEmpty()) {
715 if (members
.size() == 0) {
718 if (avatar
.isEmpty()) {
721 return sendUpdateGroupMessage(groupId
, name
, members
, avatar
);
725 * Change the expiration timer for a thread (number of groupId)
727 * @param numberOrGroupId
728 * @param messageExpirationTimer
730 public void setExpirationTimer(String numberOrGroupId
, int messageExpirationTimer
) {
731 ThreadInfo thread
= account
.getThreadStore().getThread(numberOrGroupId
);
732 thread
.messageExpirationTime
= messageExpirationTimer
;
733 account
.getThreadStore().updateThread(thread
);
736 private void requestSyncGroups() throws IOException
{
737 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
).build();
738 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
740 sendSyncMessage(message
);
741 } catch (UntrustedIdentityException e
) {
746 private void requestSyncContacts() throws IOException
{
747 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
).build();
748 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
750 sendSyncMessage(message
);
751 } catch (UntrustedIdentityException e
) {
756 private void requestSyncBlocked() throws IOException
{
757 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.BLOCKED
).build();
758 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
760 sendSyncMessage(message
);
761 } catch (UntrustedIdentityException e
) {
766 private void requestSyncConfiguration() throws IOException
{
767 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONFIGURATION
).build();
768 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
770 sendSyncMessage(message
);
771 } catch (UntrustedIdentityException e
) {
776 private byte[] getSelfUnidentifiedAccessKey() {
777 return UnidentifiedAccess
.deriveAccessKeyFrom(account
.getProfileKey());
780 private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient
) {
785 private Optional
<UnidentifiedAccessPair
> getAccessForSync() {
787 return Optional
.absent();
790 private List
<Optional
<UnidentifiedAccessPair
>> getAccessFor(Collection
<SignalServiceAddress
> recipients
) {
791 List
<Optional
<UnidentifiedAccessPair
>> result
= new ArrayList
<>(recipients
.size());
792 for (SignalServiceAddress recipient
: recipients
) {
793 result
.add(Optional
.<UnidentifiedAccessPair
>absent());
798 private Optional
<UnidentifiedAccessPair
> getAccessFor(SignalServiceAddress recipient
) {
800 return Optional
.absent();
803 private void sendSyncMessage(SignalServiceSyncMessage message
)
804 throws IOException
, UntrustedIdentityException
{
805 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(BaseConfig
.serviceConfiguration
, null, username
, account
.getPassword(),
806 account
.getDeviceId(), account
.getSignalProtocolStore(), BaseConfig
.USER_AGENT
, account
.isMultiDevice(), Optional
.fromNullable(messagePipe
), Optional
.fromNullable(unidentifiedMessagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
808 messageSender
.sendMessage(message
, getAccessForSync());
809 } catch (UntrustedIdentityException e
) {
810 account
.getSignalProtocolStore().saveIdentity(e
.getIdentifier(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
816 * This method throws an EncapsulatedExceptions exception instead of returning a list of SendMessageResult.
818 private void sendMessageLegacy(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
819 throws EncapsulatedExceptions
, IOException
{
820 List
<SendMessageResult
> results
= sendMessage(messageBuilder
, recipients
);
822 List
<UntrustedIdentityException
> untrustedIdentities
= new LinkedList
<>();
823 List
<UnregisteredUserException
> unregisteredUsers
= new LinkedList
<>();
824 List
<NetworkFailureException
> networkExceptions
= new LinkedList
<>();
826 for (SendMessageResult result
: results
) {
827 if (result
.isUnregisteredFailure()) {
828 unregisteredUsers
.add(new UnregisteredUserException(result
.getAddress().getNumber().get(), null));
829 } else if (result
.isNetworkFailure()) {
830 networkExceptions
.add(new NetworkFailureException(result
.getAddress().getNumber().get(), null));
831 } else if (result
.getIdentityFailure() != null) {
832 untrustedIdentities
.add(new UntrustedIdentityException("Untrusted", result
.getAddress().getNumber().get(), result
.getIdentityFailure().getIdentityKey()));
835 if (!untrustedIdentities
.isEmpty() || !unregisteredUsers
.isEmpty() || !networkExceptions
.isEmpty()) {
836 throw new EncapsulatedExceptions(untrustedIdentities
, unregisteredUsers
, networkExceptions
);
840 private List
<SendMessageResult
> sendMessage(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
842 Set
<SignalServiceAddress
> recipientsTS
= Utils
.getSignalServiceAddresses(recipients
, username
);
843 if (recipientsTS
== null) {
845 return Collections
.emptyList();
848 SignalServiceDataMessage message
= null;
850 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(BaseConfig
.serviceConfiguration
, null, username
, account
.getPassword(),
851 account
.getDeviceId(), account
.getSignalProtocolStore(), BaseConfig
.USER_AGENT
, account
.isMultiDevice(), Optional
.fromNullable(messagePipe
), Optional
.fromNullable(unidentifiedMessagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
853 message
= messageBuilder
.build();
854 if (message
.getGroupInfo().isPresent()) {
856 final boolean isRecipientUpdate
= false;
857 List
<SendMessageResult
> result
= messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), getAccessFor(recipientsTS
), isRecipientUpdate
, message
);
858 for (SendMessageResult r
: result
) {
859 if (r
.getIdentityFailure() != null) {
860 account
.getSignalProtocolStore().saveIdentity(r
.getAddress().getNumber().get(), r
.getIdentityFailure().getIdentityKey(), TrustLevel
.UNTRUSTED
);
864 } catch (UntrustedIdentityException e
) {
865 account
.getSignalProtocolStore().saveIdentity(e
.getIdentifier(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
866 return Collections
.emptyList();
868 } else if (recipientsTS
.size() == 1 && recipientsTS
.contains(new SignalServiceAddress(null, username
))) {
869 SignalServiceAddress recipient
= new SignalServiceAddress(null, username
);
870 final Optional
<UnidentifiedAccessPair
> unidentifiedAccess
= getAccessFor(recipient
);
871 SentTranscriptMessage transcript
= new SentTranscriptMessage(Optional
.of(new SignalServiceAddress(null, recipient
.getNumber().get())),
872 message
.getTimestamp(),
874 message
.getExpiresInSeconds(),
875 Collections
.singletonMap(new SignalServiceAddress(null, recipient
.getNumber()), unidentifiedAccess
.isPresent()),
877 SignalServiceSyncMessage syncMessage
= SignalServiceSyncMessage
.forSentTranscript(transcript
);
879 List
<SendMessageResult
> results
= new ArrayList
<>(recipientsTS
.size());
881 messageSender
.sendMessage(syncMessage
, unidentifiedAccess
);
882 } catch (UntrustedIdentityException e
) {
883 account
.getSignalProtocolStore().saveIdentity(e
.getIdentifier(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
884 results
.add(SendMessageResult
.identityFailure(recipient
, e
.getIdentityKey()));
888 // Send to all individually, so sync messages are sent correctly
889 List
<SendMessageResult
> results
= new ArrayList
<>(recipientsTS
.size());
890 for (SignalServiceAddress address
: recipientsTS
) {
891 ThreadInfo thread
= account
.getThreadStore().getThread(address
.getNumber().get());
892 if (thread
!= null) {
893 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
895 messageBuilder
.withExpiration(0);
897 message
= messageBuilder
.build();
899 SendMessageResult result
= messageSender
.sendMessage(address
, getAccessFor(address
), message
);
901 } catch (UntrustedIdentityException e
) {
902 account
.getSignalProtocolStore().saveIdentity(e
.getIdentifier(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
903 results
.add(SendMessageResult
.identityFailure(address
, e
.getIdentityKey()));
909 if (message
!= null && message
.isEndSession()) {
910 for (SignalServiceAddress recipient
: recipientsTS
) {
911 handleEndSession(recipient
.getNumber().get());
918 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws InvalidMetadataMessageException
, ProtocolInvalidMessageException
, ProtocolDuplicateMessageException
, ProtocolLegacyMessageException
, ProtocolInvalidKeyIdException
, InvalidMetadataVersionException
, ProtocolInvalidVersionException
, ProtocolNoSessionException
, ProtocolInvalidKeyException
, ProtocolUntrustedIdentityException
, SelfSendException
, UnsupportedDataMessageException
{
919 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(null, username
), account
.getSignalProtocolStore(), Utils
.getCertificateValidator());
921 return cipher
.decrypt(envelope
);
922 } catch (ProtocolUntrustedIdentityException e
) {
923 // TODO We don't get the new untrusted identity from ProtocolUntrustedIdentityException anymore ... we need to get it from somewhere else
924 // account.getSignalProtocolStore().saveIdentity(e.getSender(), e.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
929 private void handleEndSession(String source
) {
930 account
.getSignalProtocolStore().deleteAllSessions(source
);
933 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
, boolean ignoreAttachments
) {
935 if (message
.getGroupInfo().isPresent()) {
936 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
937 threadId
= Base64
.encodeBytes(groupInfo
.getGroupId());
938 GroupInfo group
= account
.getGroupStore().getGroup(groupInfo
.getGroupId());
939 switch (groupInfo
.getType()) {
942 group
= new GroupInfo(groupInfo
.getGroupId());
945 if (groupInfo
.getAvatar().isPresent()) {
946 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
947 if (avatar
.isPointer()) {
949 retrieveGroupAvatarAttachment(avatar
.asPointer(), group
.groupId
);
950 } catch (IOException
| InvalidMessageException e
) {
951 System
.err
.println("Failed to retrieve group avatar (" + avatar
.asPointer().getId() + "): " + e
.getMessage());
956 if (groupInfo
.getName().isPresent()) {
957 group
.name
= groupInfo
.getName().get();
960 if (groupInfo
.getMembers().isPresent()) {
961 List
<String
> members
= new ArrayList
<>(groupInfo
.getMembers().get().size());
962 for (SignalServiceAddress address
: groupInfo
.getMembers().get()) {
963 members
.add(address
.getNumber().get());
965 group
.members
.addAll(members
);
968 account
.getGroupStore().updateGroup(group
);
973 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
974 } catch (IOException
| EncapsulatedExceptions e
) {
982 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
983 } catch (IOException
| EncapsulatedExceptions e
) {
987 group
.members
.remove(source
);
988 account
.getGroupStore().updateGroup(group
);
994 sendUpdateGroupMessage(groupInfo
.getGroupId(), source
);
995 } catch (IOException
| EncapsulatedExceptions e
) {
997 } catch (NotAGroupMemberException e
) {
998 // We have left this group, so don't send a group update message
1005 threadId
= destination
;
1010 if (message
.isEndSession()) {
1011 handleEndSession(isSync ? destination
: source
);
1013 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1014 ThreadInfo thread
= account
.getThreadStore().getThread(threadId
);
1015 if (thread
== null) {
1016 thread
= new ThreadInfo();
1017 thread
.id
= threadId
;
1019 if (thread
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1020 thread
.messageExpirationTime
= message
.getExpiresInSeconds();
1021 account
.getThreadStore().updateThread(thread
);
1024 if (message
.getAttachments().isPresent() && !ignoreAttachments
) {
1025 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
1026 if (attachment
.isPointer()) {
1028 retrieveAttachment(attachment
.asPointer());
1029 } catch (IOException
| InvalidMessageException e
) {
1030 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
1035 if (message
.getProfileKey().isPresent() && message
.getProfileKey().get().length
== 32) {
1036 if (source
.equals(username
)) {
1037 this.account
.setProfileKey(message
.getProfileKey().get());
1039 ContactInfo contact
= account
.getContactStore().getContact(source
);
1040 if (contact
== null) {
1041 contact
= new ContactInfo();
1042 contact
.number
= source
;
1044 contact
.profileKey
= Base64
.encodeBytes(message
.getProfileKey().get());
1048 private void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1049 final File cachePath
= new File(getMessageCachePath());
1050 if (!cachePath
.exists()) {
1053 for (final File dir
: Objects
.requireNonNull(cachePath
.listFiles())) {
1054 if (!dir
.isDirectory()) {
1058 for (final File fileEntry
: Objects
.requireNonNull(dir
.listFiles())) {
1059 if (!fileEntry
.isFile()) {
1062 SignalServiceEnvelope envelope
;
1064 envelope
= Utils
.loadEnvelope(fileEntry
);
1065 if (envelope
== null) {
1068 } catch (IOException e
) {
1069 e
.printStackTrace();
1072 SignalServiceContent content
= null;
1073 if (!envelope
.isReceipt()) {
1075 content
= decryptMessage(envelope
);
1076 } catch (Exception e
) {
1079 handleMessage(envelope
, content
, ignoreAttachments
);
1082 handler
.handleMessage(envelope
, content
, null);
1084 Files
.delete(fileEntry
.toPath());
1085 } catch (IOException e
) {
1086 System
.err
.println("Failed to delete cached message file “" + fileEntry
+ "”: " + e
.getMessage());
1089 // Try to delete directory if empty
1094 public void receiveMessages(long timeout
, TimeUnit unit
, boolean returnOnTimeout
, boolean ignoreAttachments
, ReceiveMessageHandler handler
) throws IOException
{
1095 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1096 final SignalServiceMessageReceiver messageReceiver
= getMessageReceiver();
1099 if (messagePipe
== null) {
1100 messagePipe
= messageReceiver
.createMessagePipe();
1104 SignalServiceEnvelope envelope
;
1105 SignalServiceContent content
= null;
1106 Exception exception
= null;
1107 final long now
= new Date().getTime();
1109 envelope
= messagePipe
.read(timeout
, unit
, new SignalServiceMessagePipe
.MessagePipeCallback() {
1111 public void onMessage(SignalServiceEnvelope envelope
) {
1112 // store message on disk, before acknowledging receipt to the server
1114 File cacheFile
= getMessageCacheFile(envelope
.getSourceE164().get(), now
, envelope
.getTimestamp());
1115 Utils
.storeEnvelope(envelope
, cacheFile
);
1116 } catch (IOException e
) {
1117 System
.err
.println("Failed to store encrypted message in disk cache, ignoring: " + e
.getMessage());
1121 } catch (TimeoutException e
) {
1122 if (returnOnTimeout
)
1125 } catch (InvalidVersionException e
) {
1126 System
.err
.println("Ignoring error: " + e
.getMessage());
1129 if (!envelope
.isReceipt()) {
1131 content
= decryptMessage(envelope
);
1132 } catch (Exception e
) {
1135 handleMessage(envelope
, content
, ignoreAttachments
);
1138 handler
.handleMessage(envelope
, content
, exception
);
1139 if (!(exception
instanceof ProtocolUntrustedIdentityException
)) {
1140 File cacheFile
= null;
1142 cacheFile
= getMessageCacheFile(envelope
.getSourceE164().get(), now
, envelope
.getTimestamp());
1143 Files
.delete(cacheFile
.toPath());
1144 // Try to delete directory if empty
1145 new File(getMessageCachePath()).delete();
1146 } catch (IOException e
) {
1147 System
.err
.println("Failed to delete cached message file “" + cacheFile
+ "”: " + e
.getMessage());
1152 if (messagePipe
!= null) {
1153 messagePipe
.shutdown();
1159 private void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
) {
1160 if (content
!= null) {
1161 if (content
.getDataMessage().isPresent()) {
1162 SignalServiceDataMessage message
= content
.getDataMessage().get();
1163 handleSignalServiceDataMessage(message
, false, envelope
.getSourceE164().get(), username
, ignoreAttachments
);
1165 if (content
.getSyncMessage().isPresent()) {
1166 account
.setMultiDevice(true);
1167 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
1168 if (syncMessage
.getSent().isPresent()) {
1169 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
1170 handleSignalServiceDataMessage(message
, true, envelope
.getSourceE164().get(), syncMessage
.getSent().get().getDestination().get().getNumber().get(), ignoreAttachments
);
1172 if (syncMessage
.getRequest().isPresent()) {
1173 RequestMessage rm
= syncMessage
.getRequest().get();
1174 if (rm
.isContactsRequest()) {
1177 } catch (UntrustedIdentityException
| IOException e
) {
1178 e
.printStackTrace();
1181 if (rm
.isGroupsRequest()) {
1184 } catch (UntrustedIdentityException
| IOException e
) {
1185 e
.printStackTrace();
1188 // TODO Handle rm.isBlockedListRequest(); rm.isConfigurationRequest();
1190 if (syncMessage
.getGroups().isPresent()) {
1191 File tmpFile
= null;
1193 tmpFile
= IOUtils
.createTempFile();
1194 try (InputStream attachmentAsStream
= retrieveAttachmentAsStream(syncMessage
.getGroups().get().asPointer(), tmpFile
)) {
1195 DeviceGroupsInputStream s
= new DeviceGroupsInputStream(attachmentAsStream
);
1197 while ((g
= s
.read()) != null) {
1198 GroupInfo syncGroup
= account
.getGroupStore().getGroup(g
.getId());
1199 if (syncGroup
== null) {
1200 syncGroup
= new GroupInfo(g
.getId());
1202 if (g
.getName().isPresent()) {
1203 syncGroup
.name
= g
.getName().get();
1205 List
<String
> members
= new ArrayList
<>(g
.getMembers().size());
1206 for (SignalServiceAddress member
: g
.getMembers()) {
1207 members
.add(member
.getNumber().get());
1209 syncGroup
.members
.addAll(members
);
1210 syncGroup
.active
= g
.isActive();
1211 if (g
.getColor().isPresent()) {
1212 syncGroup
.color
= g
.getColor().get();
1215 if (g
.getAvatar().isPresent()) {
1216 retrieveGroupAvatarAttachment(g
.getAvatar().get(), syncGroup
.groupId
);
1218 account
.getGroupStore().updateGroup(syncGroup
);
1221 } catch (Exception e
) {
1222 e
.printStackTrace();
1224 if (tmpFile
!= null) {
1226 Files
.delete(tmpFile
.toPath());
1227 } catch (IOException e
) {
1228 System
.err
.println("Failed to delete received groups temp file “" + tmpFile
+ "”: " + e
.getMessage());
1233 if (syncMessage
.getBlockedList().isPresent()) {
1234 // TODO store list of blocked numbers
1236 if (syncMessage
.getContacts().isPresent()) {
1237 File tmpFile
= null;
1239 tmpFile
= IOUtils
.createTempFile();
1240 final ContactsMessage contactsMessage
= syncMessage
.getContacts().get();
1241 try (InputStream attachmentAsStream
= retrieveAttachmentAsStream(contactsMessage
.getContactsStream().asPointer(), tmpFile
)) {
1242 DeviceContactsInputStream s
= new DeviceContactsInputStream(attachmentAsStream
);
1243 if (contactsMessage
.isComplete()) {
1244 account
.getContactStore().clear();
1247 while ((c
= s
.read()) != null) {
1248 if (c
.getAddress().getNumber().get().equals(account
.getUsername()) && c
.getProfileKey().isPresent()) {
1249 account
.setProfileKey(c
.getProfileKey().get());
1251 ContactInfo contact
= account
.getContactStore().getContact(c
.getAddress().getNumber().get());
1252 if (contact
== null) {
1253 contact
= new ContactInfo();
1254 contact
.number
= c
.getAddress().getNumber().get();
1256 if (c
.getName().isPresent()) {
1257 contact
.name
= c
.getName().get();
1259 if (c
.getColor().isPresent()) {
1260 contact
.color
= c
.getColor().get();
1262 if (c
.getProfileKey().isPresent()) {
1263 contact
.profileKey
= Base64
.encodeBytes(c
.getProfileKey().get());
1265 if (c
.getVerified().isPresent()) {
1266 final VerifiedMessage verifiedMessage
= c
.getVerified().get();
1267 account
.getSignalProtocolStore().saveIdentity(verifiedMessage
.getDestination().getNumber().get(), verifiedMessage
.getIdentityKey(), TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
1269 if (c
.getExpirationTimer().isPresent()) {
1270 ThreadInfo thread
= account
.getThreadStore().getThread(c
.getAddress().getNumber().get());
1271 if (thread
== null) {
1272 thread
= new ThreadInfo();
1273 thread
.id
= c
.getAddress().getNumber().get();
1275 thread
.messageExpirationTime
= c
.getExpirationTimer().get();
1276 account
.getThreadStore().updateThread(thread
);
1278 if (c
.isBlocked()) {
1279 // TODO store list of blocked numbers
1281 account
.getContactStore().updateContact(contact
);
1283 if (c
.getAvatar().isPresent()) {
1284 retrieveContactAvatarAttachment(c
.getAvatar().get(), contact
.number
);
1288 } catch (Exception e
) {
1289 e
.printStackTrace();
1291 if (tmpFile
!= null) {
1293 Files
.delete(tmpFile
.toPath());
1294 } catch (IOException e
) {
1295 System
.err
.println("Failed to delete received contacts temp file “" + tmpFile
+ "”: " + e
.getMessage());
1300 if (syncMessage
.getVerified().isPresent()) {
1301 final VerifiedMessage verifiedMessage
= syncMessage
.getVerified().get();
1302 account
.getSignalProtocolStore().saveIdentity(verifiedMessage
.getDestination().getNumber().get(), verifiedMessage
.getIdentityKey(), TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
1304 if (syncMessage
.getConfiguration().isPresent()) {
1311 private File
getContactAvatarFile(String number
) {
1312 return new File(avatarsPath
, "contact-" + number
);
1315 private File
retrieveContactAvatarAttachment(SignalServiceAttachment attachment
, String number
) throws IOException
, InvalidMessageException
{
1316 IOUtils
.createPrivateDirectories(avatarsPath
);
1317 if (attachment
.isPointer()) {
1318 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1319 return retrieveAttachment(pointer
, getContactAvatarFile(number
), false);
1321 SignalServiceAttachmentStream stream
= attachment
.asStream();
1322 return Utils
.retrieveAttachment(stream
, getContactAvatarFile(number
));
1326 private File
getGroupAvatarFile(byte[] groupId
) {
1327 return new File(avatarsPath
, "group-" + Base64
.encodeBytes(groupId
).replace("/", "_"));
1330 private File
retrieveGroupAvatarAttachment(SignalServiceAttachment attachment
, byte[] groupId
) throws IOException
, InvalidMessageException
{
1331 IOUtils
.createPrivateDirectories(avatarsPath
);
1332 if (attachment
.isPointer()) {
1333 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1334 return retrieveAttachment(pointer
, getGroupAvatarFile(groupId
), false);
1336 SignalServiceAttachmentStream stream
= attachment
.asStream();
1337 return Utils
.retrieveAttachment(stream
, getGroupAvatarFile(groupId
));
1341 public File
getAttachmentFile(long attachmentId
) {
1342 return new File(attachmentsPath
, attachmentId
+ "");
1345 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
1346 IOUtils
.createPrivateDirectories(attachmentsPath
);
1347 return retrieveAttachment(pointer
, getAttachmentFile(pointer
.getId()), true);
1350 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
, File outputFile
, boolean storePreview
) throws IOException
, InvalidMessageException
{
1351 if (storePreview
&& pointer
.getPreview().isPresent()) {
1352 File previewFile
= new File(outputFile
+ ".preview");
1353 try (OutputStream output
= new FileOutputStream(previewFile
)) {
1354 byte[] preview
= pointer
.getPreview().get();
1355 output
.write(preview
, 0, preview
.length
);
1356 } catch (FileNotFoundException e
) {
1357 e
.printStackTrace();
1362 final SignalServiceMessageReceiver messageReceiver
= getMessageReceiver();
1364 File tmpFile
= IOUtils
.createTempFile();
1365 try (InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
, BaseConfig
.MAX_ATTACHMENT_SIZE
)) {
1366 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1367 byte[] buffer
= new byte[4096];
1370 while ((read
= input
.read(buffer
)) != -1) {
1371 output
.write(buffer
, 0, read
);
1373 } catch (FileNotFoundException e
) {
1374 e
.printStackTrace();
1379 Files
.delete(tmpFile
.toPath());
1380 } catch (IOException e
) {
1381 System
.err
.println("Failed to delete received attachment temp file “" + tmpFile
+ "”: " + e
.getMessage());
1387 private InputStream
retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer
, File tmpFile
) throws IOException
, InvalidMessageException
{
1388 final SignalServiceMessageReceiver messageReceiver
= getMessageReceiver();
1389 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, BaseConfig
.MAX_ATTACHMENT_SIZE
);
1393 public boolean isRemote() {
1397 private void sendGroups() throws IOException
, UntrustedIdentityException
{
1398 File groupsFile
= IOUtils
.createTempFile();
1401 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
1402 DeviceGroupsOutputStream out
= new DeviceGroupsOutputStream(fos
);
1403 for (GroupInfo
record : account
.getGroupStore().getGroups()) {
1404 ThreadInfo info
= account
.getThreadStore().getThread(Base64
.encodeBytes(record.groupId
));
1405 List
<SignalServiceAddress
> members
= new ArrayList
<>(record.members
.size());
1406 for (String member
: record.members
) {
1407 members
.add(new SignalServiceAddress(null, member
));
1409 out
.write(new DeviceGroup(record.groupId
, Optional
.fromNullable(record.name
),
1410 members
, createGroupAvatarAttachment(record.groupId
),
1411 record.active
, Optional
.fromNullable(info
!= null ? info
.messageExpirationTime
: null),
1412 Optional
.fromNullable(record.color
), false));
1416 if (groupsFile
.exists() && groupsFile
.length() > 0) {
1417 try (FileInputStream groupsFileStream
= new FileInputStream(groupsFile
)) {
1418 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1419 .withStream(groupsFileStream
)
1420 .withContentType("application/octet-stream")
1421 .withLength(groupsFile
.length())
1424 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
1429 Files
.delete(groupsFile
.toPath());
1430 } catch (IOException e
) {
1431 System
.err
.println("Failed to delete groups temp file “" + groupsFile
+ "”: " + e
.getMessage());
1436 public void sendContacts() throws IOException
, UntrustedIdentityException
{
1437 File contactsFile
= IOUtils
.createTempFile();
1440 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
1441 DeviceContactsOutputStream out
= new DeviceContactsOutputStream(fos
);
1442 for (ContactInfo
record : account
.getContactStore().getContacts()) {
1443 VerifiedMessage verifiedMessage
= null;
1444 ThreadInfo info
= account
.getThreadStore().getThread(record.number
);
1445 if (getIdentities().containsKey(record.number
)) {
1446 JsonIdentityKeyStore
.Identity currentIdentity
= null;
1447 for (JsonIdentityKeyStore
.Identity id
: getIdentities().get(record.number
)) {
1448 if (currentIdentity
== null || id
.getDateAdded().after(currentIdentity
.getDateAdded())) {
1449 currentIdentity
= id
;
1452 if (currentIdentity
!= null) {
1453 verifiedMessage
= new VerifiedMessage(new SignalServiceAddress(null, record.number
), currentIdentity
.getIdentityKey(), currentIdentity
.getTrustLevel().toVerifiedState(), currentIdentity
.getDateAdded().getTime());
1457 byte[] profileKey
= record.profileKey
== null ?
null : Base64
.decode(record.profileKey
);
1458 // TODO store list of blocked numbers
1459 boolean blocked
= false;
1460 out
.write(new DeviceContact(new SignalServiceAddress(null, record.number
), Optional
.fromNullable(record.name
),
1461 createContactAvatarAttachment(record.number
), Optional
.fromNullable(record.color
),
1462 Optional
.fromNullable(verifiedMessage
), Optional
.fromNullable(profileKey
), blocked
, Optional
.fromNullable(info
!= null ? info
.messageExpirationTime
: null)));
1465 if (account
.getProfileKey() != null) {
1466 // Send our own profile key as well
1467 out
.write(new DeviceContact(new SignalServiceAddress(null, account
.getUsername()),
1468 Optional
.<String
>absent(), Optional
.<SignalServiceAttachmentStream
>absent(),
1469 Optional
.<String
>absent(), Optional
.<VerifiedMessage
>absent(),
1470 Optional
.of(account
.getProfileKey()),
1471 false, Optional
.<Integer
>absent()));
1475 if (contactsFile
.exists() && contactsFile
.length() > 0) {
1476 try (FileInputStream contactsFileStream
= new FileInputStream(contactsFile
)) {
1477 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1478 .withStream(contactsFileStream
)
1479 .withContentType("application/octet-stream")
1480 .withLength(contactsFile
.length())
1483 sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
, true)));
1488 Files
.delete(contactsFile
.toPath());
1489 } catch (IOException e
) {
1490 System
.err
.println("Failed to delete contacts temp file “" + contactsFile
+ "”: " + e
.getMessage());
1495 private void sendVerifiedMessage(String destination
, IdentityKey identityKey
, TrustLevel trustLevel
) throws IOException
, UntrustedIdentityException
{
1496 VerifiedMessage verifiedMessage
= new VerifiedMessage(new SignalServiceAddress(null, destination
), identityKey
, trustLevel
.toVerifiedState(), System
.currentTimeMillis());
1497 sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
1500 public List
<ContactInfo
> getContacts() {
1501 return account
.getContactStore().getContacts();
1504 public ContactInfo
getContact(String number
) {
1505 return account
.getContactStore().getContact(number
);
1508 public GroupInfo
getGroup(byte[] groupId
) {
1509 return account
.getGroupStore().getGroup(groupId
);
1512 public Map
<String
, List
<JsonIdentityKeyStore
.Identity
>> getIdentities() {
1513 return account
.getSignalProtocolStore().getIdentities();
1516 public List
<JsonIdentityKeyStore
.Identity
> getIdentities(String number
) {
1517 return account
.getSignalProtocolStore().getIdentities(number
);
1521 * Trust this the identity with this fingerprint
1523 * @param name username of the identity
1524 * @param fingerprint Fingerprint
1526 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) {
1527 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1531 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1532 if (!Arrays
.equals(id
.getIdentityKey().serialize(), fingerprint
)) {
1536 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1538 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1539 } catch (IOException
| UntrustedIdentityException e
) {
1540 e
.printStackTrace();
1549 * Trust this the identity with this safety number
1551 * @param name username of the identity
1552 * @param safetyNumber Safety number
1554 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) {
1555 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1559 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1560 if (!safetyNumber
.equals(computeSafetyNumber(name
, id
.getIdentityKey()))) {
1564 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1566 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1567 } catch (IOException
| UntrustedIdentityException e
) {
1568 e
.printStackTrace();
1577 * Trust all keys of this identity without verification
1579 * @param name username of the identity
1581 public boolean trustIdentityAllKeys(String name
) {
1582 List
<JsonIdentityKeyStore
.Identity
> ids
= account
.getSignalProtocolStore().getIdentities(name
);
1586 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1587 if (id
.getTrustLevel() == TrustLevel
.UNTRUSTED
) {
1588 account
.getSignalProtocolStore().saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1590 sendVerifiedMessage(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1591 } catch (IOException
| UntrustedIdentityException e
) {
1592 e
.printStackTrace();
1600 public String
computeSafetyNumber(String theirUsername
, IdentityKey theirIdentityKey
) {
1601 return Utils
.computeSafetyNumber(username
, getIdentity(), theirUsername
, theirIdentityKey
);
1604 public interface ReceiveMessageHandler
{
1606 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);