1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.api
.Contact
;
4 import org
.asamk
.signal
.manager
.api
.GroupId
;
5 import org
.asamk
.signal
.manager
.api
.MessageEnvelope
.Sync
.MessageRequestResponse
;
6 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
7 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
8 import org
.asamk
.signal
.manager
.storage
.groups
.GroupInfoV1
;
9 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientAddress
;
10 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientId
;
11 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPack
;
12 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
13 import org
.asamk
.signal
.manager
.util
.IOUtils
;
14 import org
.asamk
.signal
.manager
.util
.MimeUtils
;
15 import org
.jetbrains
.annotations
.NotNull
;
16 import org
.signal
.libsignal
.protocol
.IdentityKey
;
17 import org
.slf4j
.Logger
;
18 import org
.slf4j
.LoggerFactory
;
19 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
20 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
21 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
22 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.BlockedListMessage
;
23 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ConfigurationMessage
;
24 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.ContactsMessage
;
25 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContact
;
26 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsInputStream
;
27 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceContactsOutputStream
;
28 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroup
;
29 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsInputStream
;
30 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.DeviceGroupsOutputStream
;
31 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.KeysMessage
;
32 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.MessageRequestResponseMessage
;
33 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.RequestMessage
;
34 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
35 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.StickerPackOperationMessage
;
36 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.VerifiedMessage
;
37 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
38 import org
.whispersystems
.signalservice
.internal
.push
.SyncMessage
;
39 import org
.whispersystems
.signalservice
.internal
.push
.http
.ResumableUploadSpec
;
41 import java
.io
.FileInputStream
;
42 import java
.io
.FileOutputStream
;
43 import java
.io
.IOException
;
44 import java
.io
.InputStream
;
45 import java
.io
.OutputStream
;
46 import java
.nio
.file
.Files
;
47 import java
.util
.ArrayList
;
48 import java
.util
.List
;
49 import java
.util
.Optional
;
50 import java
.util
.stream
.Collectors
;
51 import java
.util
.stream
.Stream
;
53 public class SyncHelper
{
55 private static final Logger logger
= LoggerFactory
.getLogger(SyncHelper
.class);
57 private final Context context
;
58 private final SignalAccount account
;
60 public SyncHelper(final Context context
) {
61 this.context
= context
;
62 this.account
= context
.getAccount();
65 public void requestAllSyncData() {
66 requestSyncData(SyncMessage
.Request
.Type
.GROUPS
);
67 requestSyncData(SyncMessage
.Request
.Type
.CONTACTS
);
68 requestSyncData(SyncMessage
.Request
.Type
.BLOCKED
);
69 requestSyncData(SyncMessage
.Request
.Type
.CONFIGURATION
);
71 requestSyncPniIdentity();
74 public void requestSyncKeys() {
75 requestSyncData(SyncMessage
.Request
.Type
.KEYS
);
78 public void requestSyncPniIdentity() {
79 requestSyncData(SyncMessage
.Request
.Type
.PNI_IDENTITY
);
82 public SendMessageResult
sendSyncFetchProfileMessage() {
83 return context
.getSendHelper()
84 .sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.LOCAL_PROFILE
));
87 public void sendSyncFetchStorageMessage() {
88 context
.getSendHelper()
89 .sendSyncMessage(SignalServiceSyncMessage
.forFetchLatest(SignalServiceSyncMessage
.FetchType
.STORAGE_MANIFEST
));
92 public void sendGroups() throws IOException
{
93 var groupsFile
= IOUtils
.createTempFile();
96 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
97 var out
= new DeviceGroupsOutputStream(fos
);
98 for (var record : account
.getGroupStore().getGroups()) {
99 if (record instanceof GroupInfoV1 groupInfo
) {
100 out
.write(new DeviceGroup(groupInfo
.getGroupId().serialize(),
101 Optional
.ofNullable(groupInfo
.name
),
102 groupInfo
.getMembers()
104 .map(context
.getRecipientHelper()::resolveSignalServiceAddress
)
106 context
.getGroupHelper().createGroupAvatarAttachment(groupInfo
.getGroupId()),
107 groupInfo
.isMember(account
.getSelfRecipientId()),
108 Optional
.of(groupInfo
.messageExpirationTime
),
109 Optional
.ofNullable(groupInfo
.color
),
112 groupInfo
.archived
));
117 if (groupsFile
.exists() && groupsFile
.length() > 0) {
118 try (var groupsFileStream
= new FileInputStream(groupsFile
)) {
119 final var uploadSpec
= context
.getDependencies()
121 .getResumableUploadSpec()
123 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
124 .withStream(groupsFileStream
)
125 .withContentType(MimeUtils
.OCTET_STREAM
)
126 .withLength(groupsFile
.length())
127 .withResumableUploadSpec(ResumableUploadSpec
.from(uploadSpec
))
130 context
.getSendHelper().sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
135 Files
.delete(groupsFile
.toPath());
136 } catch (IOException e
) {
137 logger
.warn("Failed to delete groups temp file “{}”, ignoring: {}", groupsFile
, e
.getMessage());
142 public void sendContacts() throws IOException
{
143 var contactsFile
= IOUtils
.createTempFile();
146 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
147 var out
= new DeviceContactsOutputStream(fos
);
148 for (var contactPair
: account
.getContactStore().getContacts()) {
149 final var recipientId
= contactPair
.first();
150 final var contact
= contactPair
.second();
151 final var address
= account
.getRecipientAddressResolver().resolveRecipientAddress(recipientId
);
153 out
.write(getDeviceContact(address
, recipientId
, contact
));
156 if (account
.getProfileKey() != null) {
157 // Send our own profile key as well
158 final var address
= account
.getSelfRecipientAddress();
159 final var recipientId
= account
.getSelfRecipientId();
160 final var contact
= account
.getContactStore().getContact(recipientId
);
161 out
.write(getDeviceContact(address
, recipientId
, contact
));
165 if (contactsFile
.exists() && contactsFile
.length() > 0) {
166 try (var contactsFileStream
= new FileInputStream(contactsFile
)) {
167 final var uploadSpec
= context
.getDependencies()
169 .getResumableUploadSpec()
171 var attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
172 .withStream(contactsFileStream
)
173 .withContentType(MimeUtils
.OCTET_STREAM
)
174 .withLength(contactsFile
.length())
175 .withResumableUploadSpec(ResumableUploadSpec
.from(uploadSpec
))
178 context
.getSendHelper()
179 .sendSyncMessage(SignalServiceSyncMessage
.forContacts(new ContactsMessage(attachmentStream
,
185 Files
.delete(contactsFile
.toPath());
186 } catch (IOException e
) {
187 logger
.warn("Failed to delete contacts temp file “{}”, ignoring: {}", contactsFile
, e
.getMessage());
193 private DeviceContact
getDeviceContact(
194 final RecipientAddress address
, final RecipientId recipientId
, final Contact contact
195 ) throws IOException
{
196 var currentIdentity
= address
.serviceId().isEmpty()
198 : account
.getIdentityKeyStore().getIdentityInfo(address
.serviceId().get());
199 VerifiedMessage verifiedMessage
= null;
200 if (currentIdentity
!= null) {
201 verifiedMessage
= new VerifiedMessage(address
.toSignalServiceAddress(),
202 currentIdentity
.getIdentityKey(),
203 currentIdentity
.getTrustLevel().toVerifiedState(),
204 currentIdentity
.getDateAddedTimestamp());
207 var profileKey
= account
.getProfileStore().getProfileKey(recipientId
);
208 return new DeviceContact(address
.aci(),
210 Optional
.ofNullable(contact
== null ?
null : contact
.getName()),
211 createContactAvatarAttachment(address
),
212 Optional
.ofNullable(contact
== null ?
null : contact
.color()),
213 Optional
.ofNullable(verifiedMessage
),
214 Optional
.ofNullable(profileKey
),
215 Optional
.ofNullable(contact
== null ?
null : contact
.messageExpirationTime()),
217 contact
!= null && contact
.isArchived());
220 public SendMessageResult
sendBlockedList() {
221 var addresses
= new ArrayList
<SignalServiceAddress
>();
222 for (var record : account
.getContactStore().getContacts()) {
223 if (record.second().isBlocked()) {
224 addresses
.add(context
.getRecipientHelper().resolveSignalServiceAddress(record.first()));
227 var groupIds
= new ArrayList
<byte[]>();
228 for (var record : account
.getGroupStore().getGroups()) {
229 if (record.isBlocked()) {
230 groupIds
.add(record.getGroupId().serialize());
233 return context
.getSendHelper()
234 .sendSyncMessage(SignalServiceSyncMessage
.forBlocked(new BlockedListMessage(addresses
, groupIds
)));
237 public SendMessageResult
sendVerifiedMessage(
238 SignalServiceAddress destination
, IdentityKey identityKey
, TrustLevel trustLevel
240 var verifiedMessage
= new VerifiedMessage(destination
,
242 trustLevel
.toVerifiedState(),
243 System
.currentTimeMillis());
244 return context
.getSendHelper().sendSyncMessage(SignalServiceSyncMessage
.forVerified(verifiedMessage
));
247 public SendMessageResult
sendKeysMessage() {
248 var keysMessage
= new KeysMessage(Optional
.ofNullable(account
.getOrCreateStorageKey()),
249 Optional
.ofNullable(account
.getOrCreatePinMasterKey()));
250 return context
.getSendHelper().sendSyncMessage(SignalServiceSyncMessage
.forKeys(keysMessage
));
253 public SendMessageResult
sendStickerOperationsMessage(
254 List
<StickerPack
> installStickers
, List
<StickerPack
> removeStickers
256 var installStickerMessages
= installStickers
.stream().map(s
-> getStickerPackOperationMessage(s
, true));
257 var removeStickerMessages
= removeStickers
.stream().map(s
-> getStickerPackOperationMessage(s
, false));
258 var stickerMessages
= Stream
.concat(installStickerMessages
, removeStickerMessages
).toList();
259 return context
.getSendHelper()
260 .sendSyncMessage(SignalServiceSyncMessage
.forStickerPackOperations(stickerMessages
));
263 private static StickerPackOperationMessage
getStickerPackOperationMessage(
264 final StickerPack s
, final boolean installed
266 return new StickerPackOperationMessage(s
.packId().serialize(),
268 installed ? StickerPackOperationMessage
.Type
.INSTALL
: StickerPackOperationMessage
.Type
.REMOVE
);
271 public SendMessageResult
sendConfigurationMessage() {
272 final var config
= account
.getConfigurationStore();
273 var configurationMessage
= new ConfigurationMessage(Optional
.ofNullable(config
.getReadReceipts()),
274 Optional
.ofNullable(config
.getUnidentifiedDeliveryIndicators()),
275 Optional
.ofNullable(config
.getTypingIndicators()),
276 Optional
.ofNullable(config
.getLinkPreviews()));
277 return context
.getSendHelper().sendSyncMessage(SignalServiceSyncMessage
.forConfiguration(configurationMessage
));
280 public void handleSyncDeviceGroups(final InputStream input
) {
281 final var s
= new DeviceGroupsInputStream(input
);
286 } catch (IOException e
) {
287 logger
.warn("Sync groups contained invalid group, ignoring: {}", e
.getMessage());
293 var syncGroup
= account
.getGroupStore().getOrCreateGroupV1(GroupId
.v1(g
.getId()));
294 if (syncGroup
!= null) {
295 if (g
.getName().isPresent()) {
296 syncGroup
.name
= g
.getName().get();
298 syncGroup
.addMembers(g
.getMembers()
300 .map(account
.getRecipientResolver()::resolveRecipient
)
301 .collect(Collectors
.toSet()));
303 syncGroup
.removeMember(account
.getSelfRecipientId());
305 // Add ourself to the member set as it's marked as active
306 syncGroup
.addMembers(List
.of(account
.getSelfRecipientId()));
308 syncGroup
.blocked
= g
.isBlocked();
309 if (g
.getColor().isPresent()) {
310 syncGroup
.color
= g
.getColor().get();
313 if (g
.getAvatar().isPresent()) {
314 context
.getGroupHelper().downloadGroupAvatar(syncGroup
.getGroupId(), g
.getAvatar().get());
316 syncGroup
.archived
= g
.isArchived();
317 account
.getGroupStore().updateGroup(syncGroup
);
322 public void handleSyncDeviceContacts(final InputStream input
) throws IOException
{
323 final var s
= new DeviceContactsInputStream(input
);
328 } catch (IOException e
) {
329 if (e
.getMessage() != null && e
.getMessage().contains("Missing contact address!")) {
330 logger
.warn("Sync contacts contained invalid contact, ignoring: {}", e
.getMessage());
336 if (c
== null || (c
.getAci().isEmpty() && c
.getE164().isEmpty())) {
339 final var address
= new RecipientAddress(c
.getAci(), Optional
.empty(), c
.getE164(), Optional
.empty());
340 if (address
.matches(account
.getSelfRecipientAddress()) && c
.getProfileKey().isPresent()) {
341 account
.setProfileKey(c
.getProfileKey().get());
343 final var recipientId
= account
.getRecipientTrustedResolver().resolveRecipientTrusted(address
);
344 var contact
= account
.getContactStore().getContact(recipientId
);
345 final var builder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
346 if (c
.getName().isPresent() && (
348 contact
.givenName() == null && contact
.familyName() == null
351 builder
.withGivenName(c
.getName().get());
352 builder
.withFamilyName(null);
354 if (c
.getColor().isPresent()) {
355 builder
.withColor(c
.getColor().get());
357 if (c
.getProfileKey().isPresent()) {
358 account
.getProfileStore().storeProfileKey(recipientId
, c
.getProfileKey().get());
360 if (c
.getVerified().isPresent()) {
361 final var verifiedMessage
= c
.getVerified().get();
362 account
.getIdentityKeyStore()
363 .setIdentityTrustLevel(verifiedMessage
.getDestination().getServiceId(),
364 verifiedMessage
.getIdentityKey(),
365 TrustLevel
.fromVerifiedState(verifiedMessage
.getVerified()));
367 if (c
.getExpirationTimer().isPresent()) {
368 builder
.withMessageExpirationTime(c
.getExpirationTimer().get());
370 builder
.withIsArchived(c
.isArchived());
371 account
.getContactStore().storeContact(recipientId
, builder
.build());
373 if (c
.getAvatar().isPresent()) {
374 downloadContactAvatar(c
.getAvatar().get(), address
);
379 public SendMessageResult
sendMessageRequestResponse(
380 final MessageRequestResponse
.Type type
, final GroupId groupId
382 final var response
= MessageRequestResponseMessage
.forGroup(groupId
.serialize(), localToRemoteType(type
));
383 return context
.getSendHelper().sendSyncMessage(SignalServiceSyncMessage
.forMessageRequestResponse(response
));
386 public SendMessageResult
sendMessageRequestResponse(
387 final MessageRequestResponse
.Type type
, final RecipientId recipientId
389 final var address
= account
.getRecipientAddressResolver().resolveRecipientAddress(recipientId
);
390 if (address
.serviceId().isEmpty()) {
393 context
.getContactHelper()
394 .setContactProfileSharing(recipientId
,
395 type
== MessageRequestResponse
.Type
.ACCEPT
396 || type
== MessageRequestResponse
.Type
.UNBLOCK_AND_ACCEPT
);
397 final var response
= MessageRequestResponseMessage
.forIndividual(address
.serviceId().get(),
398 localToRemoteType(type
));
399 return context
.getSendHelper().sendSyncMessage(SignalServiceSyncMessage
.forMessageRequestResponse(response
));
402 private SendMessageResult
requestSyncData(final SyncMessage
.Request
.Type type
) {
403 var r
= new SyncMessage
.Request
.Builder().type(type
).build();
404 var message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
405 return context
.getSendHelper().sendSyncMessage(message
);
408 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(RecipientAddress address
) throws IOException
{
409 final var streamDetails
= context
.getAvatarStore().retrieveContactAvatar(address
);
410 if (streamDetails
== null) {
411 return Optional
.empty();
414 final var uploadSpec
= context
.getDependencies().getMessageSender().getResumableUploadSpec().toProto();
415 return Optional
.of(AttachmentUtils
.createAttachmentStream(streamDetails
, Optional
.empty(), uploadSpec
));
418 private void downloadContactAvatar(SignalServiceAttachment avatar
, RecipientAddress address
) {
420 context
.getAvatarStore()
421 .storeContactAvatar(address
,
422 outputStream
-> context
.getAttachmentHelper().retrieveAttachment(avatar
, outputStream
));
423 } catch (IOException e
) {
424 logger
.warn("Failed to download avatar for contact {}, ignoring: {}", address
, e
.getMessage());
428 private MessageRequestResponseMessage
.Type
localToRemoteType(final MessageRequestResponse
.Type type
) {
429 return switch (type
) {
430 case UNKNOWN
-> MessageRequestResponseMessage
.Type
.UNKNOWN
;
431 case ACCEPT
-> MessageRequestResponseMessage
.Type
.ACCEPT
;
432 case DELETE
-> MessageRequestResponseMessage
.Type
.DELETE
;
433 case BLOCK
-> MessageRequestResponseMessage
.Type
.BLOCK
;
434 case BLOCK_AND_DELETE
-> MessageRequestResponseMessage
.Type
.BLOCK_AND_DELETE
;
435 case UNBLOCK_AND_ACCEPT
-> MessageRequestResponseMessage
.Type
.UNBLOCK_AND_ACCEPT
;
436 case SPAM
-> MessageRequestResponseMessage
.Type
.SPAM
;
437 case BLOCK_AND_SPAM
-> MessageRequestResponseMessage
.Type
.BLOCK_AND_SPAM
;