]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/Manager.java
Improve stderr output
[signal-cli] / src / main / java / org / asamk / signal / manager / Manager.java
1 /*
2 Copyright (C) 2015-2020 AsamK and contributors
3
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.
8
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.
13
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/>.
16 */
17 package org.asamk.signal.manager;
18
19 import com.fasterxml.jackson.databind.ObjectMapper;
20
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.util.IOUtils;
27 import org.asamk.signal.util.Util;
28 import org.signal.libsignal.metadata.InvalidMetadataMessageException;
29 import org.signal.libsignal.metadata.InvalidMetadataVersionException;
30 import org.signal.libsignal.metadata.ProtocolDuplicateMessageException;
31 import org.signal.libsignal.metadata.ProtocolInvalidKeyException;
32 import org.signal.libsignal.metadata.ProtocolInvalidKeyIdException;
33 import org.signal.libsignal.metadata.ProtocolInvalidMessageException;
34 import org.signal.libsignal.metadata.ProtocolInvalidVersionException;
35 import org.signal.libsignal.metadata.ProtocolLegacyMessageException;
36 import org.signal.libsignal.metadata.ProtocolNoSessionException;
37 import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
38 import org.signal.libsignal.metadata.SelfSendException;
39 import org.signal.libsignal.metadata.certificate.InvalidCertificateException;
40 import org.signal.zkgroup.InvalidInputException;
41 import org.signal.zkgroup.profiles.ClientZkProfileOperations;
42 import org.signal.zkgroup.profiles.ProfileKey;
43 import org.whispersystems.libsignal.IdentityKey;
44 import org.whispersystems.libsignal.IdentityKeyPair;
45 import org.whispersystems.libsignal.InvalidKeyException;
46 import org.whispersystems.libsignal.InvalidMessageException;
47 import org.whispersystems.libsignal.InvalidVersionException;
48 import org.whispersystems.libsignal.ecc.Curve;
49 import org.whispersystems.libsignal.ecc.ECKeyPair;
50 import org.whispersystems.libsignal.ecc.ECPublicKey;
51 import org.whispersystems.libsignal.state.PreKeyRecord;
52 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
53 import org.whispersystems.libsignal.util.KeyHelper;
54 import org.whispersystems.libsignal.util.Medium;
55 import org.whispersystems.libsignal.util.Pair;
56 import org.whispersystems.libsignal.util.guava.Optional;
57 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
58 import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
59 import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
60 import org.whispersystems.signalservice.api.SignalServiceMessageSender;
61 import org.whispersystems.signalservice.api.crypto.InvalidCiphertextException;
62 import org.whispersystems.signalservice.api.crypto.ProfileCipher;
63 import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
64 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
65 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
66 import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
67 import org.whispersystems.signalservice.api.messages.SendMessageResult;
68 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
69 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
70 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
71 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
72 import org.whispersystems.signalservice.api.messages.SignalServiceContent;
73 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
74 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
75 import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
76 import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
77 import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload;
78 import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload.StickerInfo;
79 import org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage;
80 import org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage;
81 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContact;
82 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsInputStream;
83 import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream;
84 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup;
85 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsInputStream;
86 import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream;
87 import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo;
88 import org.whispersystems.signalservice.api.messages.multidevice.RequestMessage;
89 import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
90 import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
91 import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
92 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
93 import org.whispersystems.signalservice.api.push.ContactTokenDetails;
94 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
95 import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
96 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
97 import org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException;
98 import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
99 import org.whispersystems.signalservice.api.util.InvalidNumberException;
100 import org.whispersystems.signalservice.api.util.SleepTimer;
101 import org.whispersystems.signalservice.api.util.StreamDetails;
102 import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
103 import org.whispersystems.signalservice.api.util.UuidUtil;
104 import org.whispersystems.signalservice.internal.configuration.SignalServiceConfiguration;
105 import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
106 import org.whispersystems.signalservice.internal.push.UnsupportedDataMessageException;
107 import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
108 import org.whispersystems.signalservice.internal.util.Hex;
109 import org.whispersystems.util.Base64;
110
111 import java.io.Closeable;
112 import java.io.File;
113 import java.io.FileInputStream;
114 import java.io.FileNotFoundException;
115 import java.io.FileOutputStream;
116 import java.io.IOException;
117 import java.io.InputStream;
118 import java.io.OutputStream;
119 import java.net.URI;
120 import java.net.URISyntaxException;
121 import java.net.URLEncoder;
122 import java.nio.file.Files;
123 import java.nio.file.Paths;
124 import java.nio.file.StandardCopyOption;
125 import java.util.ArrayList;
126 import java.util.Arrays;
127 import java.util.Collection;
128 import java.util.Collections;
129 import java.util.Date;
130 import java.util.HashSet;
131 import java.util.LinkedList;
132 import java.util.List;
133 import java.util.Locale;
134 import java.util.Objects;
135 import java.util.Set;
136 import java.util.UUID;
137 import java.util.concurrent.ExecutionException;
138 import java.util.concurrent.ExecutorService;
139 import java.util.concurrent.TimeUnit;
140 import java.util.concurrent.TimeoutException;
141 import java.util.stream.Collectors;
142 import java.util.zip.ZipEntry;
143 import java.util.zip.ZipFile;
144
145 public class Manager implements Closeable {
146
147 private final SleepTimer timer = new UptimeSleepTimer();
148 private final SignalServiceConfiguration serviceConfiguration;
149 private final String userAgent;
150
151 private final SignalAccount account;
152 private final PathConfig pathConfig;
153 private SignalServiceAccountManager accountManager;
154 private SignalServiceMessagePipe messagePipe = null;
155 private SignalServiceMessagePipe unidentifiedMessagePipe = null;
156
157 public Manager(SignalAccount account, PathConfig pathConfig, SignalServiceConfiguration serviceConfiguration, String userAgent) {
158 this.account = account;
159 this.pathConfig = pathConfig;
160 this.serviceConfiguration = serviceConfiguration;
161 this.userAgent = userAgent;
162 this.accountManager = createSignalServiceAccountManager();
163
164 this.account.setResolver(this::resolveSignalServiceAddress);
165 }
166
167 public String getUsername() {
168 return account.getUsername();
169 }
170
171 public SignalServiceAddress getSelfAddress() {
172 return account.getSelfAddress();
173 }
174
175 private SignalServiceAccountManager createSignalServiceAccountManager() {
176 return new SignalServiceAccountManager(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), userAgent, timer);
177 }
178
179 private IdentityKeyPair getIdentityKeyPair() {
180 return account.getSignalProtocolStore().getIdentityKeyPair();
181 }
182
183 public int getDeviceId() {
184 return account.getDeviceId();
185 }
186
187 private String getMessageCachePath() {
188 return pathConfig.getDataPath() + "/" + account.getUsername() + ".d/msg-cache";
189 }
190
191 private String getMessageCachePath(String sender) {
192 if (sender == null || sender.isEmpty()) {
193 return getMessageCachePath();
194 }
195
196 return getMessageCachePath() + "/" + sender.replace("/", "_");
197 }
198
199 private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException {
200 String cachePath = getMessageCachePath(sender);
201 IOUtils.createPrivateDirectories(cachePath);
202 return new File(cachePath + "/" + now + "_" + timestamp);
203 }
204
205 public static Manager init(String username, String settingsPath, SignalServiceConfiguration serviceConfiguration, String userAgent) throws IOException {
206 PathConfig pathConfig = PathConfig.createDefault(settingsPath);
207
208 if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) {
209 IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
210 int registrationId = KeyHelper.generateRegistrationId(false);
211
212 ProfileKey profileKey = KeyUtils.createProfileKey();
213 SignalAccount account = SignalAccount.create(pathConfig.getDataPath(), username, identityKey, registrationId, profileKey);
214 account.save();
215
216 return new Manager(account, pathConfig, serviceConfiguration, userAgent);
217 }
218
219 SignalAccount account = SignalAccount.load(pathConfig.getDataPath(), username);
220
221 Manager m = new Manager(account, pathConfig, serviceConfiguration, userAgent);
222
223 m.migrateLegacyConfigs();
224
225 return m;
226 }
227
228 private void migrateLegacyConfigs() {
229 // Copy group avatars that were previously stored in the attachments folder
230 // to the new avatar folder
231 if (JsonGroupStore.groupsWithLegacyAvatarId.size() > 0) {
232 for (GroupInfo g : JsonGroupStore.groupsWithLegacyAvatarId) {
233 File avatarFile = getGroupAvatarFile(g.groupId);
234 File attachmentFile = getAttachmentFile(new SignalServiceAttachmentRemoteId(g.getAvatarId()));
235 if (!avatarFile.exists() && attachmentFile.exists()) {
236 try {
237 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
238 Files.copy(attachmentFile.toPath(), avatarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
239 } catch (Exception e) {
240 // Ignore
241 }
242 }
243 }
244 JsonGroupStore.groupsWithLegacyAvatarId.clear();
245 account.save();
246 }
247 if (account.getProfileKey() == null) {
248 // Old config file, creating new profile key
249 account.setProfileKey(KeyUtils.createProfileKey());
250 account.save();
251 }
252 }
253
254 public void checkAccountState() throws IOException {
255 if (account.isRegistered()) {
256 if (accountManager.getPreKeysCount() < ServiceConfig.PREKEY_MINIMUM_COUNT) {
257 refreshPreKeys();
258 account.save();
259 }
260 if (account.getUuid() == null) {
261 account.setUuid(accountManager.getOwnUuid());
262 account.save();
263 }
264 }
265 }
266
267 public boolean isRegistered() {
268 return account.isRegistered();
269 }
270
271 public void register(boolean voiceVerification) throws IOException {
272 account.setPassword(KeyUtils.createPassword());
273
274 // Resetting UUID, because registering doesn't work otherwise
275 account.setUuid(null);
276 accountManager = createSignalServiceAccountManager();
277
278 if (voiceVerification) {
279 accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.absent(), Optional.absent());
280 } else {
281 accountManager.requestSmsVerificationCode(false, Optional.absent(), Optional.absent());
282 }
283
284 account.setRegistered(false);
285 account.save();
286 }
287
288 public void updateAccountAttributes() throws IOException {
289 accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), account.getRegistrationLock(), getSelfUnidentifiedAccessKey(), false, ServiceConfig.capabilities);
290 }
291
292 public void setProfile(String name, File avatar) throws IOException {
293 try (final StreamDetails streamDetails = avatar == null ? null : Utils.createStreamDetailsFromFile(avatar)) {
294 accountManager.setVersionedProfile(account.getUuid(), account.getProfileKey(), name, streamDetails);
295 }
296 }
297
298 public void unregister() throws IOException {
299 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
300 // If this is the master device, other users can't send messages to this number anymore.
301 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
302 accountManager.setGcmId(Optional.absent());
303
304 account.setRegistered(false);
305 account.save();
306 }
307
308 public List<DeviceInfo> getLinkedDevices() throws IOException {
309 List<DeviceInfo> devices = accountManager.getDevices();
310 account.setMultiDevice(devices.size() > 1);
311 account.save();
312 return devices;
313 }
314
315 public void removeLinkedDevices(int deviceId) throws IOException {
316 accountManager.removeDevice(deviceId);
317 List<DeviceInfo> devices = accountManager.getDevices();
318 account.setMultiDevice(devices.size() > 1);
319 account.save();
320 }
321
322 public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
323 Utils.DeviceLinkInfo info = Utils.parseDeviceLinkUri(linkUri);
324
325 addDevice(info.deviceIdentifier, info.deviceKey);
326 }
327
328 private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
329 IdentityKeyPair identityKeyPair = getIdentityKeyPair();
330 String verificationCode = accountManager.getNewDeviceVerificationCode();
331
332 accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, Optional.of(account.getProfileKey().serialize()), verificationCode);
333 account.setMultiDevice(true);
334 account.save();
335 }
336
337 private List<PreKeyRecord> generatePreKeys() {
338 List<PreKeyRecord> records = new ArrayList<>(ServiceConfig.PREKEY_BATCH_SIZE);
339
340 final int offset = account.getPreKeyIdOffset();
341 for (int i = 0; i < ServiceConfig.PREKEY_BATCH_SIZE; i++) {
342 int preKeyId = (offset + i) % Medium.MAX_VALUE;
343 ECKeyPair keyPair = Curve.generateKeyPair();
344 PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
345
346 records.add(record);
347 }
348
349 account.addPreKeys(records);
350 account.save();
351
352 return records;
353 }
354
355 private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
356 try {
357 ECKeyPair keyPair = Curve.generateKeyPair();
358 byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
359 SignedPreKeyRecord record = new SignedPreKeyRecord(account.getNextSignedPreKeyId(), System.currentTimeMillis(), keyPair, signature);
360
361 account.addSignedPreKey(record);
362 account.save();
363
364 return record;
365 } catch (InvalidKeyException e) {
366 throw new AssertionError(e);
367 }
368 }
369
370 public void verifyAccount(String verificationCode, String pin) throws IOException {
371 verificationCode = verificationCode.replace("-", "");
372 account.setSignalingKey(KeyUtils.createSignalingKey());
373 // TODO make unrestricted unidentified access configurable
374 VerifyAccountResponse response = accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, null, getSelfUnidentifiedAccessKey(), false, ServiceConfig.capabilities);
375
376 UUID uuid = UuidUtil.parseOrNull(response.getUuid());
377 // TODO response.isStorageCapable()
378 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
379 account.setRegistered(true);
380 account.setUuid(uuid);
381 account.setRegistrationLockPin(pin);
382 account.getSignalProtocolStore().saveIdentity(account.getSelfAddress(), getIdentityKeyPair().getPublicKey(), TrustLevel.TRUSTED_VERIFIED);
383
384 refreshPreKeys();
385 account.save();
386 }
387
388 public void setRegistrationLockPin(Optional<String> pin) throws IOException {
389 if (pin.isPresent()) {
390 account.setRegistrationLockPin(pin.get());
391 throw new RuntimeException("Not implemented anymore, will be replaced with KBS");
392 } else {
393 account.setRegistrationLockPin(null);
394 accountManager.removeRegistrationLockV1();
395 }
396 account.save();
397 }
398
399 void refreshPreKeys() throws IOException {
400 List<PreKeyRecord> oneTimePreKeys = generatePreKeys();
401 final IdentityKeyPair identityKeyPair = getIdentityKeyPair();
402 SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(identityKeyPair);
403
404 accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKeyRecord, oneTimePreKeys);
405 }
406
407 private SignalServiceMessageReceiver getMessageReceiver() {
408 // TODO implement ZkGroup support
409 final ClientZkProfileOperations clientZkProfileOperations = null;
410 return new SignalServiceMessageReceiver(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(), account.getDeviceId(), account.getSignalingKey(), userAgent, null, timer, clientZkProfileOperations);
411 }
412
413 private SignalServiceMessageSender getMessageSender() {
414 // TODO implement ZkGroup support
415 final ClientZkProfileOperations clientZkProfileOperations = null;
416 final boolean attachmentsV3 = false;
417 final ExecutorService executor = null;
418 return new SignalServiceMessageSender(serviceConfiguration, account.getUuid(), account.getUsername(), account.getPassword(),
419 account.getDeviceId(), account.getSignalProtocolStore(), userAgent, account.isMultiDevice(), attachmentsV3, Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent(), clientZkProfileOperations, executor);
420 }
421
422 private SignalServiceProfile getEncryptedRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess) throws IOException {
423 SignalServiceMessagePipe pipe = unidentifiedMessagePipe != null && unidentifiedAccess.isPresent() ? unidentifiedMessagePipe
424 : messagePipe;
425
426 if (pipe != null) {
427 try {
428 return pipe.getProfile(address, Optional.absent(), unidentifiedAccess, SignalServiceProfile.RequestType.PROFILE).get(10, TimeUnit.SECONDS).getProfile();
429 } catch (IOException | InterruptedException | ExecutionException | TimeoutException ignored) {
430 }
431 }
432
433 SignalServiceMessageReceiver receiver = getMessageReceiver();
434 try {
435 return receiver.retrieveProfile(address, Optional.absent(), unidentifiedAccess, SignalServiceProfile.RequestType.PROFILE).get(10, TimeUnit.SECONDS).getProfile();
436 } catch (InterruptedException | ExecutionException | TimeoutException e) {
437 throw new IOException("Failed to retrieve profile", e);
438 }
439 }
440
441 private SignalProfile getRecipientProfile(SignalServiceAddress address, Optional<UnidentifiedAccess> unidentifiedAccess, ProfileKey profileKey) throws IOException {
442 return decryptProfile(getEncryptedRecipientProfile(address, unidentifiedAccess), profileKey);
443 }
444
445 private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException {
446 File file = getGroupAvatarFile(groupId);
447 if (!file.exists()) {
448 return Optional.absent();
449 }
450
451 return Optional.of(Utils.createAttachment(file));
452 }
453
454 private Optional<SignalServiceAttachmentStream> createContactAvatarAttachment(String number) throws IOException {
455 File file = getContactAvatarFile(number);
456 if (!file.exists()) {
457 return Optional.absent();
458 }
459
460 return Optional.of(Utils.createAttachment(file));
461 }
462
463 private GroupInfo getGroupForSending(byte[] groupId) throws GroupNotFoundException, NotAGroupMemberException {
464 GroupInfo g = account.getGroupStore().getGroup(groupId);
465 if (g == null) {
466 throw new GroupNotFoundException(groupId);
467 }
468 if (!g.isMember(account.getSelfAddress())) {
469 throw new NotAGroupMemberException(groupId, g.name);
470 }
471 return g;
472 }
473
474 public List<GroupInfo> getGroups() {
475 return account.getGroupStore().getGroups();
476 }
477
478 public long sendGroupMessage(String messageText, List<String> attachments,
479 byte[] groupId)
480 throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
481 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
482 if (attachments != null) {
483 messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
484 }
485 if (groupId != null) {
486 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
487 .withId(groupId)
488 .build();
489 messageBuilder.asGroupMessage(group);
490 }
491
492 final GroupInfo g = getGroupForSending(groupId);
493
494 messageBuilder.withExpiration(g.messageExpirationTime);
495
496 return sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
497 }
498
499 public void sendGroupMessageReaction(String emoji, boolean remove, String targetAuthor,
500 long targetSentTimestamp, byte[] groupId)
501 throws IOException, EncapsulatedExceptions, InvalidNumberException, NotAGroupMemberException, GroupNotFoundException {
502 SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, canonicalizeAndResolveSignalServiceAddress(targetAuthor), targetSentTimestamp);
503 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
504 .withReaction(reaction);
505 if (groupId != null) {
506 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
507 .withId(groupId)
508 .build();
509 messageBuilder.asGroupMessage(group);
510 }
511 final GroupInfo g = getGroupForSending(groupId);
512 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
513 }
514
515 public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions, NotAGroupMemberException {
516 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
517 .withId(groupId)
518 .build();
519
520 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
521 .asGroupMessage(group);
522
523 final GroupInfo g = getGroupForSending(groupId);
524 g.removeMember(account.getSelfAddress());
525 account.getGroupStore().updateGroup(g);
526
527 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
528 }
529
530 private byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<SignalServiceAddress> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
531 GroupInfo g;
532 if (groupId == null) {
533 // Create new group
534 g = new GroupInfo(KeyUtils.createGroupId());
535 g.addMembers(Collections.singleton(account.getSelfAddress()));
536 } else {
537 g = getGroupForSending(groupId);
538 }
539
540 if (name != null) {
541 g.name = name;
542 }
543
544 if (members != null) {
545 final Set<String> newE164Members = new HashSet<>();
546 for (SignalServiceAddress member : members) {
547 if (g.isMember(member) || !member.getNumber().isPresent()) {
548 continue;
549 }
550 newE164Members.add(member.getNumber().get());
551 }
552
553 final List<ContactTokenDetails> contacts = accountManager.getContacts(newE164Members);
554 if (contacts.size() != newE164Members.size()) {
555 // Some of the new members are not registered on Signal
556 for (ContactTokenDetails contact : contacts) {
557 newE164Members.remove(contact.getNumber());
558 }
559 throw new IOException("Failed to add members " + Util.join(", ", newE164Members) + " to group: Not registered on Signal");
560 }
561
562 g.addMembers(members);
563 }
564
565 if (avatarFile != null) {
566 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
567 File aFile = getGroupAvatarFile(g.groupId);
568 Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
569 }
570
571 account.getGroupStore().updateGroup(g);
572
573 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
574
575 sendMessageLegacy(messageBuilder, g.getMembersWithout(account.getSelfAddress()));
576 return g.groupId;
577 }
578
579 void sendUpdateGroupMessage(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions, NotAGroupMemberException, GroupNotFoundException, AttachmentInvalidException {
580 if (groupId == null) {
581 return;
582 }
583 GroupInfo g = getGroupForSending(groupId);
584
585 if (!g.isMember(recipient)) {
586 return;
587 }
588
589 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
590
591 // Send group message only to the recipient who requested it
592 sendMessageLegacy(messageBuilder, Collections.singleton(recipient));
593 }
594
595 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) throws AttachmentInvalidException {
596 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
597 .withId(g.groupId)
598 .withName(g.name)
599 .withMembers(new ArrayList<>(g.getMembers()));
600
601 File aFile = getGroupAvatarFile(g.groupId);
602 if (aFile.exists()) {
603 try {
604 group.withAvatar(Utils.createAttachment(aFile));
605 } catch (IOException e) {
606 throw new AttachmentInvalidException(aFile.toString(), e);
607 }
608 }
609
610 return SignalServiceDataMessage.newBuilder()
611 .asGroupMessage(group.build())
612 .withExpiration(g.messageExpirationTime);
613 }
614
615 void sendGroupInfoRequest(byte[] groupId, SignalServiceAddress recipient) throws IOException, EncapsulatedExceptions {
616 if (groupId == null) {
617 return;
618 }
619
620 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO)
621 .withId(groupId);
622
623 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
624 .asGroupMessage(group.build());
625
626 // Send group info request message to the recipient who sent us a message with this groupId
627 sendMessageLegacy(messageBuilder, Collections.singleton(recipient));
628 }
629
630 void sendReceipt(SignalServiceAddress remoteAddress, long messageId) throws IOException, UntrustedIdentityException {
631 SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.DELIVERY,
632 Collections.singletonList(messageId),
633 System.currentTimeMillis());
634
635 getMessageSender().sendReceipt(remoteAddress, getAccessFor(remoteAddress), receiptMessage);
636 }
637
638 public long sendMessage(String messageText, List<String> attachments,
639 List<String> recipients)
640 throws IOException, EncapsulatedExceptions, AttachmentInvalidException, InvalidNumberException {
641 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
642 if (attachments != null) {
643 List<SignalServiceAttachment> attachmentStreams = Utils.getSignalServiceAttachments(attachments);
644
645 // Upload attachments here, so we only upload once even for multiple recipients
646 SignalServiceMessageSender messageSender = getMessageSender();
647 List<SignalServiceAttachment> attachmentPointers = new ArrayList<>(attachmentStreams.size());
648 for (SignalServiceAttachment attachment : attachmentStreams) {
649 if (attachment.isStream()) {
650 attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
651 } else if (attachment.isPointer()) {
652 attachmentPointers.add(attachment.asPointer());
653 }
654 }
655
656 messageBuilder.withAttachments(attachmentPointers);
657 }
658 return sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
659 }
660
661 public void sendMessageReaction(String emoji, boolean remove, String targetAuthor,
662 long targetSentTimestamp, List<String> recipients)
663 throws IOException, EncapsulatedExceptions, InvalidNumberException {
664 SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, canonicalizeAndResolveSignalServiceAddress(targetAuthor), targetSentTimestamp);
665 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
666 .withReaction(reaction);
667 sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
668 }
669
670 public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions, InvalidNumberException {
671 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
672 .asEndSessionMessage();
673
674 final Collection<SignalServiceAddress> signalServiceAddresses = getSignalServiceAddresses(recipients);
675 try {
676 sendMessageLegacy(messageBuilder, signalServiceAddresses);
677 } catch (Exception e) {
678 for (SignalServiceAddress address : signalServiceAddresses) {
679 handleEndSession(address);
680 }
681 account.save();
682 throw e;
683 }
684 }
685
686 public String getContactName(String number) throws InvalidNumberException {
687 ContactInfo contact = account.getContactStore().getContact(canonicalizeAndResolveSignalServiceAddress(number));
688 if (contact == null) {
689 return "";
690 } else {
691 return contact.name;
692 }
693 }
694
695 public void setContactName(String number, String name) throws InvalidNumberException {
696 final SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(number);
697 ContactInfo contact = account.getContactStore().getContact(address);
698 if (contact == null) {
699 contact = new ContactInfo(address);
700 }
701 contact.name = name;
702 account.getContactStore().updateContact(contact);
703 account.save();
704 }
705
706 public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException {
707 setContactBlocked(canonicalizeAndResolveSignalServiceAddress(number), blocked);
708 }
709
710 private void setContactBlocked(SignalServiceAddress address, boolean blocked) {
711 ContactInfo contact = account.getContactStore().getContact(address);
712 if (contact == null) {
713 contact = new ContactInfo(address);
714 }
715 contact.blocked = blocked;
716 account.getContactStore().updateContact(contact);
717 account.save();
718 }
719
720 public void setGroupBlocked(final byte[] groupId, final boolean blocked) throws GroupNotFoundException {
721 GroupInfo group = getGroup(groupId);
722 if (group == null) {
723 throw new GroupNotFoundException(groupId);
724 }
725
726 group.blocked = blocked;
727 account.getGroupStore().updateGroup(group);
728 account.save();
729 }
730
731 public byte[] updateGroup(byte[] groupId, String name, List<String> members, String avatar) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, InvalidNumberException, NotAGroupMemberException {
732 if (groupId.length == 0) {
733 groupId = null;
734 }
735 if (name.isEmpty()) {
736 name = null;
737 }
738 if (members.isEmpty()) {
739 members = null;
740 }
741 if (avatar.isEmpty()) {
742 avatar = null;
743 }
744 return sendUpdateGroupMessage(groupId, name, members == null ? null : getSignalServiceAddresses(members), avatar);
745 }
746
747 /**
748 * Change the expiration timer for a contact
749 */
750 public void setExpirationTimer(SignalServiceAddress address, int messageExpirationTimer) throws IOException {
751 ContactInfo contact = account.getContactStore().getContact(address);
752 contact.messageExpirationTime = messageExpirationTimer;
753 account.getContactStore().updateContact(contact);
754 sendExpirationTimerUpdate(address);
755 account.save();
756 }
757
758 private void sendExpirationTimerUpdate(SignalServiceAddress address) throws IOException {
759 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
760 .asExpirationUpdate();
761 sendMessage(messageBuilder, Collections.singleton(address));
762 }
763
764 /**
765 * Change the expiration timer for a contact
766 */
767 public void setExpirationTimer(String number, int messageExpirationTimer) throws IOException, InvalidNumberException {
768 SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(number);
769 setExpirationTimer(address, messageExpirationTimer);
770 }
771
772 /**
773 * Change the expiration timer for a group
774 */
775 public void setExpirationTimer(byte[] groupId, int messageExpirationTimer) {
776 GroupInfo g = account.getGroupStore().getGroup(groupId);
777 g.messageExpirationTime = messageExpirationTimer;
778 account.getGroupStore().updateGroup(g);
779 }
780
781 /**
782 * Upload the sticker pack from path.
783 *
784 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
785 * @return if successful, returns the URL to install the sticker pack in the signal app
786 */
787 public String uploadStickerPack(String path) throws IOException, StickerPackInvalidException {
788 SignalServiceStickerManifestUpload manifest = getSignalServiceStickerManifestUpload(path);
789
790 SignalServiceMessageSender messageSender = getMessageSender();
791
792 byte[] packKey = KeyUtils.createStickerUploadKey();
793 String packId = messageSender.uploadStickerManifest(manifest, packKey);
794
795 try {
796 return new URI("https", "signal.art", "/addstickers/", "pack_id=" + URLEncoder.encode(packId, "utf-8") + "&pack_key=" + URLEncoder.encode(Hex.toStringCondensed(packKey), "utf-8"))
797 .toString();
798 } catch (URISyntaxException e) {
799 throw new AssertionError(e);
800 }
801 }
802
803 private SignalServiceStickerManifestUpload getSignalServiceStickerManifestUpload(final String path) throws IOException, StickerPackInvalidException {
804 ZipFile zip = null;
805 String rootPath = null;
806
807 final File file = new File(path);
808 if (file.getName().endsWith(".zip")) {
809 zip = new ZipFile(file);
810 } else if (file.getName().equals("manifest.json")) {
811 rootPath = file.getParent();
812 } else {
813 throw new StickerPackInvalidException("Could not find manifest.json");
814 }
815
816 JsonStickerPack pack = parseStickerPack(rootPath, zip);
817
818 if (pack.stickers == null) {
819 throw new StickerPackInvalidException("Must set a 'stickers' field.");
820 }
821
822 if (pack.stickers.isEmpty()) {
823 throw new StickerPackInvalidException("Must include stickers.");
824 }
825
826 List<StickerInfo> stickers = new ArrayList<>(pack.stickers.size());
827 for (JsonStickerPack.JsonSticker sticker : pack.stickers) {
828 if (sticker.file == null) {
829 throw new StickerPackInvalidException("Must set a 'file' field on each sticker.");
830 }
831
832 Pair<InputStream, Long> data;
833 try {
834 data = getInputStreamAndLength(rootPath, zip, sticker.file);
835 } catch (IOException ignored) {
836 throw new StickerPackInvalidException("Could not find find " + sticker.file);
837 }
838
839 StickerInfo stickerInfo = new StickerInfo(data.first(), data.second(), Optional.fromNullable(sticker.emoji).or(""));
840 stickers.add(stickerInfo);
841 }
842
843 StickerInfo cover = null;
844 if (pack.cover != null) {
845 if (pack.cover.file == null) {
846 throw new StickerPackInvalidException("Must set a 'file' field on the cover.");
847 }
848
849 Pair<InputStream, Long> data;
850 try {
851 data = getInputStreamAndLength(rootPath, zip, pack.cover.file);
852 } catch (IOException ignored) {
853 throw new StickerPackInvalidException("Could not find find " + pack.cover.file);
854 }
855
856 cover = new StickerInfo(data.first(), data.second(), Optional.fromNullable(pack.cover.emoji).or(""));
857 }
858
859 return new SignalServiceStickerManifestUpload(
860 pack.title,
861 pack.author,
862 cover,
863 stickers);
864 }
865
866 private static JsonStickerPack parseStickerPack(String rootPath, ZipFile zip) throws IOException {
867 InputStream inputStream;
868 if (zip != null) {
869 inputStream = zip.getInputStream(zip.getEntry("manifest.json"));
870 } else {
871 inputStream = new FileInputStream((new File(rootPath, "manifest.json")));
872 }
873 return new ObjectMapper().readValue(inputStream, JsonStickerPack.class);
874 }
875
876 private static Pair<InputStream, Long> getInputStreamAndLength(final String rootPath, final ZipFile zip, final String subfile) throws IOException {
877 if (zip != null) {
878 final ZipEntry entry = zip.getEntry(subfile);
879 return new Pair<>(zip.getInputStream(entry), entry.getSize());
880 } else {
881 final File file = new File(rootPath, subfile);
882 return new Pair<>(new FileInputStream(file), file.length());
883 }
884 }
885
886 void requestSyncGroups() throws IOException {
887 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.GROUPS).build();
888 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
889 try {
890 sendSyncMessage(message);
891 } catch (UntrustedIdentityException e) {
892 e.printStackTrace();
893 }
894 }
895
896 void requestSyncContacts() throws IOException {
897 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONTACTS).build();
898 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
899 try {
900 sendSyncMessage(message);
901 } catch (UntrustedIdentityException e) {
902 e.printStackTrace();
903 }
904 }
905
906 void requestSyncBlocked() throws IOException {
907 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.BLOCKED).build();
908 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
909 try {
910 sendSyncMessage(message);
911 } catch (UntrustedIdentityException e) {
912 e.printStackTrace();
913 }
914 }
915
916 void requestSyncConfiguration() throws IOException {
917 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONFIGURATION).build();
918 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
919 try {
920 sendSyncMessage(message);
921 } catch (UntrustedIdentityException e) {
922 e.printStackTrace();
923 }
924 }
925
926 private byte[] getSenderCertificate() {
927 // TODO support UUID capable sender certificates
928 // byte[] certificate = accountManager.getSenderCertificate();
929 byte[] certificate;
930 try {
931 certificate = accountManager.getSenderCertificateLegacy();
932 } catch (IOException e) {
933 System.err.println("Failed to get sender certificate: " + e);
934 return null;
935 }
936 // TODO cache for a day
937 return certificate;
938 }
939
940 private byte[] getSelfUnidentifiedAccessKey() {
941 return UnidentifiedAccess.deriveAccessKeyFrom(account.getProfileKey());
942 }
943
944 private static SignalProfile decryptProfile(SignalServiceProfile encryptedProfile, ProfileKey profileKey) throws IOException {
945 ProfileCipher profileCipher = new ProfileCipher(profileKey);
946 try {
947 return new SignalProfile(
948 encryptedProfile.getIdentityKey(),
949 encryptedProfile.getName() == null ? null : new String(profileCipher.decryptName(Base64.decode(encryptedProfile.getName()))),
950 encryptedProfile.getAvatar(),
951 encryptedProfile.getUnidentifiedAccess() == null || !profileCipher.verifyUnidentifiedAccess(Base64.decode(encryptedProfile.getUnidentifiedAccess())) ? null : encryptedProfile.getUnidentifiedAccess(),
952 encryptedProfile.isUnrestrictedUnidentifiedAccess()
953 );
954 } catch (InvalidCiphertextException e) {
955 return null;
956 }
957 }
958
959 private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
960 ContactInfo contact = account.getContactStore().getContact(recipient);
961 if (contact == null || contact.profileKey == null) {
962 return null;
963 }
964 ProfileKey theirProfileKey;
965 try {
966 theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey));
967 } catch (InvalidInputException | IOException e) {
968 throw new AssertionError(e);
969 }
970 SignalProfile targetProfile;
971 try {
972 targetProfile = getRecipientProfile(recipient, Optional.absent(), theirProfileKey);
973 } catch (IOException e) {
974 System.err.println("Failed to get recipient profile: " + e);
975 return null;
976 }
977
978 if (targetProfile == null || targetProfile.getUnidentifiedAccess() == null) {
979 return null;
980 }
981
982 if (targetProfile.isUnrestrictedUnidentifiedAccess()) {
983 return KeyUtils.createUnrestrictedUnidentifiedAccess();
984 }
985
986 return UnidentifiedAccess.deriveAccessKeyFrom(theirProfileKey);
987 }
988
989 private Optional<UnidentifiedAccessPair> getAccessForSync() {
990 byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
991 byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();
992
993 if (selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
994 return Optional.absent();
995 }
996
997 try {
998 return Optional.of(new UnidentifiedAccessPair(
999 new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
1000 new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
1001 ));
1002 } catch (InvalidCertificateException e) {
1003 return Optional.absent();
1004 }
1005 }
1006
1007 private List<Optional<UnidentifiedAccessPair>> getAccessFor(Collection<SignalServiceAddress> recipients) {
1008 List<Optional<UnidentifiedAccessPair>> result = new ArrayList<>(recipients.size());
1009 for (SignalServiceAddress recipient : recipients) {
1010 result.add(getAccessFor(recipient));
1011 }
1012 return result;
1013 }
1014
1015 private Optional<UnidentifiedAccessPair> getAccessFor(SignalServiceAddress recipient) {
1016 byte[] recipientUnidentifiedAccessKey = getTargetUnidentifiedAccessKey(recipient);
1017 byte[] selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
1018 byte[] selfUnidentifiedAccessCertificate = getSenderCertificate();
1019
1020 if (recipientUnidentifiedAccessKey == null || selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
1021 return Optional.absent();
1022 }
1023
1024 try {
1025 return Optional.of(new UnidentifiedAccessPair(
1026 new UnidentifiedAccess(recipientUnidentifiedAccessKey, selfUnidentifiedAccessCertificate),
1027 new UnidentifiedAccess(selfUnidentifiedAccessKey, selfUnidentifiedAccessCertificate)
1028 ));
1029 } catch (InvalidCertificateException e) {
1030 return Optional.absent();
1031 }
1032 }
1033
1034 private Optional<UnidentifiedAccess> getUnidentifiedAccess(SignalServiceAddress recipient) {
1035 Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);
1036
1037 if (unidentifiedAccess.isPresent()) {
1038 return unidentifiedAccess.get().getTargetUnidentifiedAccess();
1039 }
1040
1041 return Optional.absent();
1042 }
1043
1044 private void sendSyncMessage(SignalServiceSyncMessage message)
1045 throws IOException, UntrustedIdentityException {
1046 SignalServiceMessageSender messageSender = getMessageSender();
1047 try {
1048 messageSender.sendMessage(message, getAccessForSync());
1049 } catch (UntrustedIdentityException e) {
1050 account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1051 throw e;
1052 }
1053 }
1054
1055 /**
1056 * This method throws an EncapsulatedExceptions exception instead of returning a list of SendMessageResult.
1057 */
1058 private long sendMessageLegacy(SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients)
1059 throws EncapsulatedExceptions, IOException {
1060 final long timestamp = System.currentTimeMillis();
1061 messageBuilder.withTimestamp(timestamp);
1062 List<SendMessageResult> results = sendMessage(messageBuilder, recipients);
1063
1064 List<UntrustedIdentityException> untrustedIdentities = new LinkedList<>();
1065 List<UnregisteredUserException> unregisteredUsers = new LinkedList<>();
1066 List<NetworkFailureException> networkExceptions = new LinkedList<>();
1067
1068 for (SendMessageResult result : results) {
1069 if (result.isUnregisteredFailure()) {
1070 unregisteredUsers.add(new UnregisteredUserException(result.getAddress().getLegacyIdentifier(), null));
1071 } else if (result.isNetworkFailure()) {
1072 networkExceptions.add(new NetworkFailureException(result.getAddress().getLegacyIdentifier(), null));
1073 } else if (result.getIdentityFailure() != null) {
1074 untrustedIdentities.add(new UntrustedIdentityException("Untrusted", result.getAddress().getLegacyIdentifier(), result.getIdentityFailure().getIdentityKey()));
1075 }
1076 }
1077 if (!untrustedIdentities.isEmpty() || !unregisteredUsers.isEmpty() || !networkExceptions.isEmpty()) {
1078 throw new EncapsulatedExceptions(untrustedIdentities, unregisteredUsers, networkExceptions);
1079 }
1080 return timestamp;
1081 }
1082
1083 private Collection<SignalServiceAddress> getSignalServiceAddresses(Collection<String> numbers) throws InvalidNumberException {
1084 final Set<SignalServiceAddress> signalServiceAddresses = new HashSet<>(numbers.size());
1085
1086 for (String number : numbers) {
1087 signalServiceAddresses.add(canonicalizeAndResolveSignalServiceAddress(number));
1088 }
1089 return signalServiceAddresses;
1090 }
1091
1092 private List<SendMessageResult> sendMessage(SignalServiceDataMessage.Builder messageBuilder, Collection<SignalServiceAddress> recipients)
1093 throws IOException {
1094 if (messagePipe == null) {
1095 messagePipe = getMessageReceiver().createMessagePipe();
1096 }
1097 if (unidentifiedMessagePipe == null) {
1098 unidentifiedMessagePipe = getMessageReceiver().createUnidentifiedMessagePipe();
1099 }
1100 SignalServiceDataMessage message = null;
1101 try {
1102 message = messageBuilder.build();
1103 if (message.getGroupContext().isPresent()) {
1104 try {
1105 SignalServiceMessageSender messageSender = getMessageSender();
1106 final boolean isRecipientUpdate = false;
1107 List<SendMessageResult> result = messageSender.sendMessage(new ArrayList<>(recipients), getAccessFor(recipients), isRecipientUpdate, message);
1108 for (SendMessageResult r : result) {
1109 if (r.getIdentityFailure() != null) {
1110 account.getSignalProtocolStore().saveIdentity(r.getAddress(), r.getIdentityFailure().getIdentityKey(), TrustLevel.UNTRUSTED);
1111 }
1112 }
1113 return result;
1114 } catch (UntrustedIdentityException e) {
1115 account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1116 return Collections.emptyList();
1117 }
1118 } else {
1119 // Send to all individually, so sync messages are sent correctly
1120 List<SendMessageResult> results = new ArrayList<>(recipients.size());
1121 for (SignalServiceAddress address : recipients) {
1122 ContactInfo contact = account.getContactStore().getContact(address);
1123 if (contact != null) {
1124 messageBuilder.withExpiration(contact.messageExpirationTime);
1125 messageBuilder.withProfileKey(account.getProfileKey().serialize());
1126 } else {
1127 messageBuilder.withExpiration(0);
1128 messageBuilder.withProfileKey(null);
1129 }
1130 message = messageBuilder.build();
1131 if (address.matches(account.getSelfAddress())) {
1132 results.add(sendSelfMessage(message));
1133 } else {
1134 results.add(sendMessage(address, message));
1135 }
1136 }
1137 return results;
1138 }
1139 } finally {
1140 if (message != null && message.isEndSession()) {
1141 for (SignalServiceAddress recipient : recipients) {
1142 handleEndSession(recipient);
1143 }
1144 }
1145 account.save();
1146 }
1147 }
1148
1149 private SendMessageResult sendSelfMessage(SignalServiceDataMessage message) throws IOException {
1150 SignalServiceMessageSender messageSender = getMessageSender();
1151
1152 SignalServiceAddress recipient = account.getSelfAddress();
1153
1154 final Optional<UnidentifiedAccessPair> unidentifiedAccess = getAccessFor(recipient);
1155 SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient),
1156 message.getTimestamp(),
1157 message,
1158 message.getExpiresInSeconds(),
1159 Collections.singletonMap(recipient, unidentifiedAccess.isPresent()),
1160 false);
1161 SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript);
1162
1163 try {
1164 messageSender.sendMessage(syncMessage, unidentifiedAccess);
1165 return SendMessageResult.success(recipient, unidentifiedAccess.isPresent(), false);
1166 } catch (UntrustedIdentityException e) {
1167 account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1168 return SendMessageResult.identityFailure(recipient, e.getIdentityKey());
1169 }
1170 }
1171
1172 private SendMessageResult sendMessage(SignalServiceAddress address, SignalServiceDataMessage message) throws IOException {
1173 SignalServiceMessageSender messageSender = getMessageSender();
1174
1175 try {
1176 return messageSender.sendMessage(address, getAccessFor(address), message);
1177 } catch (UntrustedIdentityException e) {
1178 account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(e.getIdentifier()), e.getIdentityKey(), TrustLevel.UNTRUSTED);
1179 return SendMessageResult.identityFailure(address, e.getIdentityKey());
1180 }
1181 }
1182
1183 private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, SelfSendException, UnsupportedDataMessageException, org.whispersystems.libsignal.UntrustedIdentityException {
1184 SignalServiceCipher cipher = new SignalServiceCipher(account.getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator());
1185 try {
1186 return cipher.decrypt(envelope);
1187 } catch (ProtocolUntrustedIdentityException e) {
1188 if (e.getCause() instanceof org.whispersystems.libsignal.UntrustedIdentityException) {
1189 org.whispersystems.libsignal.UntrustedIdentityException identityException = (org.whispersystems.libsignal.UntrustedIdentityException) e.getCause();
1190 account.getSignalProtocolStore().saveIdentity(resolveSignalServiceAddress(identityException.getName()), identityException.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
1191 throw identityException;
1192 }
1193 throw new AssertionError(e);
1194 }
1195 }
1196
1197 private void handleEndSession(SignalServiceAddress source) {
1198 account.getSignalProtocolStore().deleteAllSessions(source);
1199 }
1200
1201 private List<HandleAction> handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, SignalServiceAddress source, SignalServiceAddress destination, boolean ignoreAttachments) {
1202 List<HandleAction> actions = new ArrayList<>();
1203 if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) {
1204 SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
1205 GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId());
1206 switch (groupInfo.getType()) {
1207 case UPDATE:
1208 if (group == null) {
1209 group = new GroupInfo(groupInfo.getGroupId());
1210 }
1211
1212 if (groupInfo.getAvatar().isPresent()) {
1213 SignalServiceAttachment avatar = groupInfo.getAvatar().get();
1214 if (avatar.isPointer()) {
1215 try {
1216 retrieveGroupAvatarAttachment(avatar.asPointer(), group.groupId);
1217 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1218 System.err.println("Failed to retrieve group avatar (" + avatar.asPointer().getRemoteId() + "): " + e.getMessage());
1219 }
1220 }
1221 }
1222
1223 if (groupInfo.getName().isPresent()) {
1224 group.name = groupInfo.getName().get();
1225 }
1226
1227 if (groupInfo.getMembers().isPresent()) {
1228 group.addMembers(groupInfo.getMembers().get()
1229 .stream()
1230 .map(this::resolveSignalServiceAddress)
1231 .collect(Collectors.toSet()));
1232 }
1233
1234 account.getGroupStore().updateGroup(group);
1235 break;
1236 case DELIVER:
1237 if (group == null && !isSync) {
1238 actions.add(new SendGroupInfoRequestAction(source, groupInfo.getGroupId()));
1239 }
1240 break;
1241 case QUIT:
1242 if (group != null) {
1243 group.removeMember(source);
1244 account.getGroupStore().updateGroup(group);
1245 }
1246 break;
1247 case REQUEST_INFO:
1248 if (group != null && !isSync) {
1249 actions.add(new SendGroupUpdateAction(source, group.groupId));
1250 }
1251 break;
1252 }
1253 }
1254 final SignalServiceAddress conversationPartnerAddress = isSync ? destination : source;
1255 if (message.isEndSession()) {
1256 handleEndSession(conversationPartnerAddress);
1257 }
1258 if (message.isExpirationUpdate() || message.getBody().isPresent()) {
1259 if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) {
1260 SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
1261 GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId());
1262 if (group == null) {
1263 group = new GroupInfo(groupInfo.getGroupId());
1264 }
1265 if (group.messageExpirationTime != message.getExpiresInSeconds()) {
1266 group.messageExpirationTime = message.getExpiresInSeconds();
1267 account.getGroupStore().updateGroup(group);
1268 }
1269 } else {
1270 ContactInfo contact = account.getContactStore().getContact(conversationPartnerAddress);
1271 if (contact == null) {
1272 contact = new ContactInfo(conversationPartnerAddress);
1273 }
1274 if (contact.messageExpirationTime != message.getExpiresInSeconds()) {
1275 contact.messageExpirationTime = message.getExpiresInSeconds();
1276 account.getContactStore().updateContact(contact);
1277 }
1278 }
1279 }
1280 if (message.getAttachments().isPresent() && !ignoreAttachments) {
1281 for (SignalServiceAttachment attachment : message.getAttachments().get()) {
1282 if (attachment.isPointer()) {
1283 try {
1284 retrieveAttachment(attachment.asPointer());
1285 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1286 System.err.println("Failed to retrieve attachment (" + attachment.asPointer().getRemoteId() + "): " + e.getMessage());
1287 }
1288 }
1289 }
1290 }
1291 if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
1292 if (source.matches(account.getSelfAddress())) {
1293 try {
1294 this.account.setProfileKey(new ProfileKey(message.getProfileKey().get()));
1295 } catch (InvalidInputException ignored) {
1296 }
1297 ContactInfo contact = account.getContactStore().getContact(source);
1298 if (contact != null) {
1299 contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
1300 account.getContactStore().updateContact(contact);
1301 }
1302 } else {
1303 ContactInfo contact = account.getContactStore().getContact(source);
1304 if (contact == null) {
1305 contact = new ContactInfo(source);
1306 }
1307 contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
1308 account.getContactStore().updateContact(contact);
1309 }
1310 }
1311 if (message.getPreviews().isPresent()) {
1312 final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();
1313 for (SignalServiceDataMessage.Preview preview : previews) {
1314 if (preview.getImage().isPresent() && preview.getImage().get().isPointer()) {
1315 SignalServiceAttachmentPointer attachment = preview.getImage().get().asPointer();
1316 try {
1317 retrieveAttachment(attachment);
1318 } catch (IOException | InvalidMessageException | MissingConfigurationException e) {
1319 System.err.println("Failed to retrieve attachment (" + attachment.getRemoteId() + "): " + e.getMessage());
1320 }
1321 }
1322 }
1323 }
1324 return actions;
1325 }
1326
1327 private void retryFailedReceivedMessages(ReceiveMessageHandler handler, boolean ignoreAttachments) {
1328 final File cachePath = new File(getMessageCachePath());
1329 if (!cachePath.exists()) {
1330 return;
1331 }
1332 for (final File dir : Objects.requireNonNull(cachePath.listFiles())) {
1333 if (!dir.isDirectory()) {
1334 retryFailedReceivedMessage(handler, ignoreAttachments, dir);
1335 continue;
1336 }
1337
1338 for (final File fileEntry : Objects.requireNonNull(dir.listFiles())) {
1339 if (!fileEntry.isFile()) {
1340 continue;
1341 }
1342 retryFailedReceivedMessage(handler, ignoreAttachments, fileEntry);
1343 }
1344 // Try to delete directory if empty
1345 dir.delete();
1346 }
1347 }
1348
1349 private void retryFailedReceivedMessage(final ReceiveMessageHandler handler, final boolean ignoreAttachments, final File fileEntry) {
1350 SignalServiceEnvelope envelope;
1351 try {
1352 envelope = Utils.loadEnvelope(fileEntry);
1353 if (envelope == null) {
1354 return;
1355 }
1356 } catch (IOException e) {
1357 e.printStackTrace();
1358 return;
1359 }
1360 SignalServiceContent content = null;
1361 if (!envelope.isReceipt()) {
1362 try {
1363 content = decryptMessage(envelope);
1364 } catch (Exception e) {
1365 return;
1366 }
1367 List<HandleAction> actions = handleMessage(envelope, content, ignoreAttachments);
1368 for (HandleAction action : actions) {
1369 try {
1370 action.execute(this);
1371 } catch (Throwable e) {
1372 e.printStackTrace();
1373 }
1374 }
1375 }
1376 account.save();
1377 handler.handleMessage(envelope, content, null);
1378 try {
1379 Files.delete(fileEntry.toPath());
1380 } catch (IOException e) {
1381 System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage());
1382 }
1383 }
1384
1385 public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, boolean ignoreAttachments, ReceiveMessageHandler handler) throws IOException {
1386 retryFailedReceivedMessages(handler, ignoreAttachments);
1387 final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
1388
1389 Set<HandleAction> queuedActions = null;
1390
1391 if (messagePipe == null) {
1392 messagePipe = messageReceiver.createMessagePipe();
1393 }
1394
1395 boolean hasCaughtUpWithOldMessages = false;
1396
1397 while (true) {
1398 SignalServiceEnvelope envelope;
1399 SignalServiceContent content = null;
1400 Exception exception = null;
1401 final long now = new Date().getTime();
1402 try {
1403 Optional<SignalServiceEnvelope> result = messagePipe.readOrEmpty(timeout, unit, envelope1 -> {
1404 // store message on disk, before acknowledging receipt to the server
1405 try {
1406 String source = envelope1.getSourceE164().isPresent() ? envelope1.getSourceE164().get() : "";
1407 File cacheFile = getMessageCacheFile(source, now, envelope1.getTimestamp());
1408 Utils.storeEnvelope(envelope1, cacheFile);
1409 } catch (IOException e) {
1410 System.err.println("Failed to store encrypted message in disk cache, ignoring: " + e.getMessage());
1411 }
1412 });
1413 if (result.isPresent()) {
1414 envelope = result.get();
1415 } else {
1416 // Received indicator that server queue is empty
1417 hasCaughtUpWithOldMessages = true;
1418
1419 if (queuedActions != null) {
1420 for (HandleAction action : queuedActions) {
1421 try {
1422 action.execute(this);
1423 } catch (Throwable e) {
1424 e.printStackTrace();
1425 }
1426 }
1427 queuedActions.clear();
1428 queuedActions = null;
1429 }
1430
1431 // Continue to wait another timeout for new messages
1432 continue;
1433 }
1434 } catch (TimeoutException e) {
1435 if (returnOnTimeout)
1436 return;
1437 continue;
1438 } catch (InvalidVersionException e) {
1439 System.err.println("Ignoring error: " + e.getMessage());
1440 continue;
1441 }
1442 if (envelope.hasSource()) {
1443 // Store uuid if we don't have it already
1444 SignalServiceAddress source = envelope.getSourceAddress();
1445 resolveSignalServiceAddress(source);
1446 }
1447 if (!envelope.isReceipt()) {
1448 try {
1449 content = decryptMessage(envelope);
1450 } catch (Exception e) {
1451 exception = e;
1452 }
1453 List<HandleAction> actions = handleMessage(envelope, content, ignoreAttachments);
1454 if (hasCaughtUpWithOldMessages) {
1455 for (HandleAction action : actions) {
1456 try {
1457 action.execute(this);
1458 } catch (Throwable e) {
1459 e.printStackTrace();
1460 }
1461 }
1462 } else {
1463 if (queuedActions == null) {
1464 queuedActions = new HashSet<>();
1465 }
1466 queuedActions.addAll(actions);
1467 }
1468 }
1469 account.save();
1470 if (!isMessageBlocked(envelope, content)) {
1471 handler.handleMessage(envelope, content, exception);
1472 }
1473 if (!(exception instanceof org.whispersystems.libsignal.UntrustedIdentityException)) {
1474 File cacheFile = null;
1475 try {
1476 cacheFile = getMessageCacheFile(envelope.getSourceE164().get(), now, envelope.getTimestamp());
1477 Files.delete(cacheFile.toPath());
1478 // Try to delete directory if empty
1479 new File(getMessageCachePath()).delete();
1480 } catch (IOException e) {
1481 System.err.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage());
1482 }
1483 }
1484 }
1485 }
1486
1487 private boolean isMessageBlocked(SignalServiceEnvelope envelope, SignalServiceContent content) {
1488 SignalServiceAddress source;
1489 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
1490 source = envelope.getSourceAddress();
1491 } else if (content != null) {
1492 source = content.getSender();
1493 } else {
1494 return false;
1495 }
1496 ContactInfo sourceContact = account.getContactStore().getContact(source);
1497 if (sourceContact != null && sourceContact.blocked) {
1498 return true;
1499 }
1500
1501 if (content != null && content.getDataMessage().isPresent()) {
1502 SignalServiceDataMessage message = content.getDataMessage().get();
1503 if (message.getGroupContext().isPresent() && message.getGroupContext().get().getGroupV1().isPresent()) {
1504 SignalServiceGroup groupInfo = message.getGroupContext().get().getGroupV1().get();
1505 GroupInfo group = getGroup(groupInfo.getGroupId());
1506 if (groupInfo.getType() == SignalServiceGroup.Type.DELIVER && group != null && group.blocked) {
1507 return true;
1508 }
1509 }
1510 }
1511 return false;
1512 }
1513
1514 private List<HandleAction> handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) {
1515 List<HandleAction> actions = new ArrayList<>();
1516 if (content != null) {
1517 SignalServiceAddress sender;
1518 if (!envelope.isUnidentifiedSender() && envelope.hasSource()) {
1519 sender = envelope.getSourceAddress();
1520 } else {
1521 sender = content.getSender();
1522 }
1523 // Store uuid if we don't have it already
1524 resolveSignalServiceAddress(sender);
1525
1526 if (content.getDataMessage().isPresent()) {
1527 SignalServiceDataMessage message = content.getDataMessage().get();
1528
1529 if (content.isNeedsReceipt()) {
1530 actions.add(new SendReceiptAction(sender, message.getTimestamp()));
1531 }
1532
1533 actions.addAll(handleSignalServiceDataMessage(message, false, sender, account.getSelfAddress(), ignoreAttachments));
1534 }
1535 if (content.getSyncMessage().isPresent()) {
1536 account.setMultiDevice(true);
1537 SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
1538 if (syncMessage.getSent().isPresent()) {
1539 SentTranscriptMessage message = syncMessage.getSent().get();
1540 actions.addAll(handleSignalServiceDataMessage(message.getMessage(), true, sender, message.getDestination().orNull(), ignoreAttachments));
1541 }
1542 if (syncMessage.getRequest().isPresent()) {
1543 RequestMessage rm = syncMessage.getRequest().get();
1544 if (rm.isContactsRequest()) {
1545 actions.add(SendSyncContactsAction.create());
1546 }
1547 if (rm.isGroupsRequest()) {
1548 actions.add(SendSyncGroupsAction.create());
1549 }
1550 if (rm.isBlockedListRequest()) {
1551 actions.add(SendSyncBlockedListAction.create());
1552 }
1553 // TODO Handle rm.isConfigurationRequest();
1554 }
1555 if (syncMessage.getGroups().isPresent()) {
1556 File tmpFile = null;
1557 try {
1558 tmpFile = IOUtils.createTempFile();
1559 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)) {
1560 DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream);
1561 DeviceGroup g;
1562 while ((g = s.read()) != null) {
1563 GroupInfo syncGroup = account.getGroupStore().getGroup(g.getId());
1564 if (syncGroup == null) {
1565 syncGroup = new GroupInfo(g.getId());
1566 }
1567 if (g.getName().isPresent()) {
1568 syncGroup.name = g.getName().get();
1569 }
1570 syncGroup.addMembers(g.getMembers()
1571 .stream()
1572 .map(this::resolveSignalServiceAddress)
1573 .collect(Collectors.toSet()));
1574 if (!g.isActive()) {
1575 syncGroup.removeMember(account.getSelfAddress());
1576 } else {
1577 // Add ourself to the member set as it's marked as active
1578 syncGroup.addMembers(Collections.singleton(account.getSelfAddress()));
1579 }
1580 syncGroup.blocked = g.isBlocked();
1581 if (g.getColor().isPresent()) {
1582 syncGroup.color = g.getColor().get();
1583 }
1584
1585 if (g.getAvatar().isPresent()) {
1586 retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId);
1587 }
1588 syncGroup.inboxPosition = g.getInboxPosition().orNull();
1589 syncGroup.archived = g.isArchived();
1590 account.getGroupStore().updateGroup(syncGroup);
1591 }
1592 }
1593 } catch (Exception e) {
1594 e.printStackTrace();
1595 } finally {
1596 if (tmpFile != null) {
1597 try {
1598 Files.delete(tmpFile.toPath());
1599 } catch (IOException e) {
1600 System.err.println("Failed to delete received groups temp file “" + tmpFile + "”: " + e.getMessage());
1601 }
1602 }
1603 }
1604 }
1605 if (syncMessage.getBlockedList().isPresent()) {
1606 final BlockedListMessage blockedListMessage = syncMessage.getBlockedList().get();
1607 for (SignalServiceAddress address : blockedListMessage.getAddresses()) {
1608 setContactBlocked(resolveSignalServiceAddress(address), true);
1609 }
1610 for (byte[] groupId : blockedListMessage.getGroupIds()) {
1611 try {
1612 setGroupBlocked(groupId, true);
1613 } catch (GroupNotFoundException e) {
1614 System.err.println("BlockedListMessage contained groupID that was not found in GroupStore: " + Base64.encodeBytes(groupId));
1615 }
1616 }
1617 }
1618 if (syncMessage.getContacts().isPresent()) {
1619 File tmpFile = null;
1620 try {
1621 tmpFile = IOUtils.createTempFile();
1622 final ContactsMessage contactsMessage = syncMessage.getContacts().get();
1623 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)) {
1624 DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream);
1625 if (contactsMessage.isComplete()) {
1626 account.getContactStore().clear();
1627 }
1628 DeviceContact c;
1629 while ((c = s.read()) != null) {
1630 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
1631 account.setProfileKey(c.getProfileKey().get());
1632 }
1633 final SignalServiceAddress address = resolveSignalServiceAddress(c.getAddress());
1634 ContactInfo contact = account.getContactStore().getContact(address);
1635 if (contact == null) {
1636 contact = new ContactInfo(address);
1637 }
1638 if (c.getName().isPresent()) {
1639 contact.name = c.getName().get();
1640 }
1641 if (c.getColor().isPresent()) {
1642 contact.color = c.getColor().get();
1643 }
1644 if (c.getProfileKey().isPresent()) {
1645 contact.profileKey = Base64.encodeBytes(c.getProfileKey().get().serialize());
1646 }
1647 if (c.getVerified().isPresent()) {
1648 final VerifiedMessage verifiedMessage = c.getVerified().get();
1649 account.getSignalProtocolStore().setIdentityTrustLevel(verifiedMessage.getDestination(), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
1650 }
1651 if (c.getExpirationTimer().isPresent()) {
1652 contact.messageExpirationTime = c.getExpirationTimer().get();
1653 }
1654 contact.blocked = c.isBlocked();
1655 contact.inboxPosition = c.getInboxPosition().orNull();
1656 contact.archived = c.isArchived();
1657 account.getContactStore().updateContact(contact);
1658
1659 if (c.getAvatar().isPresent()) {
1660 retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number);
1661 }
1662 }
1663 }
1664 } catch (Exception e) {
1665 e.printStackTrace();
1666 } finally {
1667 if (tmpFile != null) {
1668 try {
1669 Files.delete(tmpFile.toPath());
1670 } catch (IOException e) {
1671 System.err.println("Failed to delete received contacts temp file “" + tmpFile + "”: " + e.getMessage());
1672 }
1673 }
1674 }
1675 }
1676 if (syncMessage.getVerified().isPresent()) {
1677 final VerifiedMessage verifiedMessage = syncMessage.getVerified().get();
1678 account.getSignalProtocolStore().setIdentityTrustLevel(resolveSignalServiceAddress(verifiedMessage.getDestination()), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
1679 }
1680 if (syncMessage.getConfiguration().isPresent()) {
1681 // TODO
1682 }
1683 }
1684 }
1685 return actions;
1686 }
1687
1688 private File getContactAvatarFile(String number) {
1689 return new File(pathConfig.getAvatarsPath(), "contact-" + number);
1690 }
1691
1692 private File retrieveContactAvatarAttachment(SignalServiceAttachment attachment, String number) throws IOException, InvalidMessageException, MissingConfigurationException {
1693 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
1694 if (attachment.isPointer()) {
1695 SignalServiceAttachmentPointer pointer = attachment.asPointer();
1696 return retrieveAttachment(pointer, getContactAvatarFile(number), false);
1697 } else {
1698 SignalServiceAttachmentStream stream = attachment.asStream();
1699 return Utils.retrieveAttachment(stream, getContactAvatarFile(number));
1700 }
1701 }
1702
1703 private File getGroupAvatarFile(byte[] groupId) {
1704 return new File(pathConfig.getAvatarsPath(), "group-" + Base64.encodeBytes(groupId).replace("/", "_"));
1705 }
1706
1707 private File retrieveGroupAvatarAttachment(SignalServiceAttachment attachment, byte[] groupId) throws IOException, InvalidMessageException, MissingConfigurationException {
1708 IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
1709 if (attachment.isPointer()) {
1710 SignalServiceAttachmentPointer pointer = attachment.asPointer();
1711 return retrieveAttachment(pointer, getGroupAvatarFile(groupId), false);
1712 } else {
1713 SignalServiceAttachmentStream stream = attachment.asStream();
1714 return Utils.retrieveAttachment(stream, getGroupAvatarFile(groupId));
1715 }
1716 }
1717
1718 public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) {
1719 return new File(pathConfig.getAttachmentsPath(), attachmentId.toString());
1720 }
1721
1722 private File retrieveAttachment(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException, MissingConfigurationException {
1723 IOUtils.createPrivateDirectories(pathConfig.getAttachmentsPath());
1724 return retrieveAttachment(pointer, getAttachmentFile(pointer.getRemoteId()), true);
1725 }
1726
1727 private File retrieveAttachment(SignalServiceAttachmentPointer pointer, File outputFile, boolean storePreview) throws IOException, InvalidMessageException, MissingConfigurationException {
1728 if (storePreview && pointer.getPreview().isPresent()) {
1729 File previewFile = new File(outputFile + ".preview");
1730 try (OutputStream output = new FileOutputStream(previewFile)) {
1731 byte[] preview = pointer.getPreview().get();
1732 output.write(preview, 0, preview.length);
1733 } catch (FileNotFoundException e) {
1734 e.printStackTrace();
1735 return null;
1736 }
1737 }
1738
1739 final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
1740
1741 File tmpFile = IOUtils.createTempFile();
1742 try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE)) {
1743 try (OutputStream output = new FileOutputStream(outputFile)) {
1744 byte[] buffer = new byte[4096];
1745 int read;
1746
1747 while ((read = input.read(buffer)) != -1) {
1748 output.write(buffer, 0, read);
1749 }
1750 } catch (FileNotFoundException e) {
1751 e.printStackTrace();
1752 return null;
1753 }
1754 } finally {
1755 try {
1756 Files.delete(tmpFile.toPath());
1757 } catch (IOException e) {
1758 System.err.println("Failed to delete received attachment temp file “" + tmpFile + "”: " + e.getMessage());
1759 }
1760 }
1761 return outputFile;
1762 }
1763
1764 private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException, MissingConfigurationException {
1765 final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
1766 return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
1767 }
1768
1769 void sendGroups() throws IOException, UntrustedIdentityException {
1770 File groupsFile = IOUtils.createTempFile();
1771
1772 try {
1773 try (OutputStream fos = new FileOutputStream(groupsFile)) {
1774 DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
1775 for (GroupInfo record : account.getGroupStore().getGroups()) {
1776 out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
1777 new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId),
1778 record.isMember(account.getSelfAddress()), Optional.of(record.messageExpirationTime),
1779 Optional.fromNullable(record.color), record.blocked, Optional.fromNullable(record.inboxPosition), record.archived));
1780 }
1781 }
1782
1783 if (groupsFile.exists() && groupsFile.length() > 0) {
1784 try (FileInputStream groupsFileStream = new FileInputStream(groupsFile)) {
1785 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
1786 .withStream(groupsFileStream)
1787 .withContentType("application/octet-stream")
1788 .withLength(groupsFile.length())
1789 .build();
1790
1791 sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
1792 }
1793 }
1794 } finally {
1795 try {
1796 Files.delete(groupsFile.toPath());
1797 } catch (IOException e) {
1798 System.err.println("Failed to delete groups temp file “" + groupsFile + "”: " + e.getMessage());
1799 }
1800 }
1801 }
1802
1803 public void sendContacts() throws IOException, UntrustedIdentityException {
1804 File contactsFile = IOUtils.createTempFile();
1805
1806 try {
1807 try (OutputStream fos = new FileOutputStream(contactsFile)) {
1808 DeviceContactsOutputStream out = new DeviceContactsOutputStream(fos);
1809 for (ContactInfo record : account.getContactStore().getContacts()) {
1810 VerifiedMessage verifiedMessage = null;
1811 JsonIdentityKeyStore.Identity currentIdentity = account.getSignalProtocolStore().getIdentity(record.getAddress());
1812 if (currentIdentity != null) {
1813 verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
1814 }
1815
1816 ProfileKey profileKey = null;
1817 try {
1818 profileKey = record.profileKey == null ? null : new ProfileKey(Base64.decode(record.profileKey));
1819 } catch (InvalidInputException ignored) {
1820 }
1821 out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name),
1822 createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
1823 Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked,
1824 Optional.of(record.messageExpirationTime),
1825 Optional.fromNullable(record.inboxPosition), record.archived));
1826 }
1827
1828 if (account.getProfileKey() != null) {
1829 // Send our own profile key as well
1830 out.write(new DeviceContact(account.getSelfAddress(),
1831 Optional.absent(), Optional.absent(),
1832 Optional.absent(), Optional.absent(),
1833 Optional.of(account.getProfileKey()),
1834 false, Optional.absent(), Optional.absent(), false));
1835 }
1836 }
1837
1838 if (contactsFile.exists() && contactsFile.length() > 0) {
1839 try (FileInputStream contactsFileStream = new FileInputStream(contactsFile)) {
1840 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
1841 .withStream(contactsFileStream)
1842 .withContentType("application/octet-stream")
1843 .withLength(contactsFile.length())
1844 .build();
1845
1846 sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true)));
1847 }
1848 }
1849 } finally {
1850 try {
1851 Files.delete(contactsFile.toPath());
1852 } catch (IOException e) {
1853 System.err.println("Failed to delete contacts temp file “" + contactsFile + "”: " + e.getMessage());
1854 }
1855 }
1856 }
1857
1858 void sendBlockedList() throws IOException, UntrustedIdentityException {
1859 List<SignalServiceAddress> addresses = new ArrayList<>();
1860 for (ContactInfo record : account.getContactStore().getContacts()) {
1861 if (record.blocked) {
1862 addresses.add(record.getAddress());
1863 }
1864 }
1865 List<byte[]> groupIds = new ArrayList<>();
1866 for (GroupInfo record : account.getGroupStore().getGroups()) {
1867 if (record.blocked) {
1868 groupIds.add(record.groupId);
1869 }
1870 }
1871 sendSyncMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(addresses, groupIds)));
1872 }
1873
1874 private void sendVerifiedMessage(SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
1875 VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis());
1876 sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
1877 }
1878
1879 public List<ContactInfo> getContacts() {
1880 return account.getContactStore().getContacts();
1881 }
1882
1883 public ContactInfo getContact(String number) {
1884 return account.getContactStore().getContact(Util.getSignalServiceAddressFromIdentifier(number));
1885 }
1886
1887 public GroupInfo getGroup(byte[] groupId) {
1888 return account.getGroupStore().getGroup(groupId);
1889 }
1890
1891 public List<JsonIdentityKeyStore.Identity> getIdentities() {
1892 return account.getSignalProtocolStore().getIdentities();
1893 }
1894
1895 public List<JsonIdentityKeyStore.Identity> getIdentities(String number) throws InvalidNumberException {
1896 return account.getSignalProtocolStore().getIdentities(canonicalizeAndResolveSignalServiceAddress(number));
1897 }
1898
1899 /**
1900 * Trust this the identity with this fingerprint
1901 *
1902 * @param name username of the identity
1903 * @param fingerprint Fingerprint
1904 */
1905 public boolean trustIdentityVerified(String name, byte[] fingerprint) throws InvalidNumberException {
1906 SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(name);
1907 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
1908 if (ids == null) {
1909 return false;
1910 }
1911 for (JsonIdentityKeyStore.Identity id : ids) {
1912 if (!Arrays.equals(id.getIdentityKey().serialize(), fingerprint)) {
1913 continue;
1914 }
1915
1916 account.getSignalProtocolStore().setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1917 try {
1918 sendVerifiedMessage(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1919 } catch (IOException | UntrustedIdentityException e) {
1920 e.printStackTrace();
1921 }
1922 account.save();
1923 return true;
1924 }
1925 return false;
1926 }
1927
1928 /**
1929 * Trust this the identity with this safety number
1930 *
1931 * @param name username of the identity
1932 * @param safetyNumber Safety number
1933 */
1934 public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) throws InvalidNumberException {
1935 SignalServiceAddress address = canonicalizeAndResolveSignalServiceAddress(name);
1936 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
1937 if (ids == null) {
1938 return false;
1939 }
1940 for (JsonIdentityKeyStore.Identity id : ids) {
1941 if (!safetyNumber.equals(computeSafetyNumber(address, id.getIdentityKey()))) {
1942 continue;
1943 }
1944
1945 account.getSignalProtocolStore().setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1946 try {
1947 sendVerifiedMessage(address, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1948 } catch (IOException | UntrustedIdentityException e) {
1949 e.printStackTrace();
1950 }
1951 account.save();
1952 return true;
1953 }
1954 return false;
1955 }
1956
1957 /**
1958 * Trust all keys of this identity without verification
1959 *
1960 * @param name username of the identity
1961 */
1962 public boolean trustIdentityAllKeys(String name) {
1963 SignalServiceAddress address = resolveSignalServiceAddress(name);
1964 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(address);
1965 if (ids == null) {
1966 return false;
1967 }
1968 for (JsonIdentityKeyStore.Identity id : ids) {
1969 if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
1970 account.getSignalProtocolStore().setIdentityTrustLevel(address, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
1971 try {
1972 sendVerifiedMessage(address, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
1973 } catch (IOException | UntrustedIdentityException e) {
1974 e.printStackTrace();
1975 }
1976 }
1977 }
1978 account.save();
1979 return true;
1980 }
1981
1982 public String computeSafetyNumber(SignalServiceAddress theirAddress, IdentityKey theirIdentityKey) {
1983 return Utils.computeSafetyNumber(account.getSelfAddress(), getIdentityKeyPair().getPublicKey(), theirAddress, theirIdentityKey);
1984 }
1985
1986 void saveAccount() {
1987 account.save();
1988 }
1989
1990 public SignalServiceAddress canonicalizeAndResolveSignalServiceAddress(String identifier) throws InvalidNumberException {
1991 String canonicalizedNumber = UuidUtil.isUuid(identifier) ? identifier : Util.canonicalizeNumber(identifier, account.getUsername());
1992 return resolveSignalServiceAddress(canonicalizedNumber);
1993 }
1994
1995 public SignalServiceAddress resolveSignalServiceAddress(String identifier) {
1996 SignalServiceAddress address = Util.getSignalServiceAddressFromIdentifier(identifier);
1997
1998 return resolveSignalServiceAddress(address);
1999 }
2000
2001 public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
2002 if (address.matches(account.getSelfAddress())) {
2003 return account.getSelfAddress();
2004 }
2005
2006 return account.getRecipientStore().resolveServiceAddress(address);
2007 }
2008
2009 @Override
2010 public void close() throws IOException {
2011 if (messagePipe != null) {
2012 messagePipe.shutdown();
2013 messagePipe = null;
2014 }
2015
2016 if (unidentifiedMessagePipe != null) {
2017 unidentifiedMessagePipe.shutdown();
2018 unidentifiedMessagePipe = null;
2019 }
2020
2021 account.close();
2022 }
2023
2024 public interface ReceiveMessageHandler {
2025
2026 void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, Throwable e);
2027 }
2028 }