]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/Manager.java
Manager : removeLinkedDevices updates isMultiDevice and saves the account
[signal-cli] / src / main / java / org / asamk / signal / manager / Manager.java
1 /*
2 Copyright (C) 2015-2018 AsamK
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 org.asamk.Signal;
20 import org.asamk.signal.*;
21 import org.asamk.signal.storage.SignalAccount;
22 import org.asamk.signal.storage.contacts.ContactInfo;
23 import org.asamk.signal.storage.groups.GroupInfo;
24 import org.asamk.signal.storage.groups.JsonGroupStore;
25 import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
26 import org.asamk.signal.storage.threads.ThreadInfo;
27 import org.asamk.signal.util.IOUtils;
28 import org.asamk.signal.util.Util;
29 import org.signal.libsignal.metadata.*;
30 import org.whispersystems.libsignal.*;
31 import org.whispersystems.libsignal.ecc.Curve;
32 import org.whispersystems.libsignal.ecc.ECKeyPair;
33 import org.whispersystems.libsignal.ecc.ECPublicKey;
34 import org.whispersystems.libsignal.state.PreKeyRecord;
35 import org.whispersystems.libsignal.state.SignedPreKeyRecord;
36 import org.whispersystems.libsignal.util.KeyHelper;
37 import org.whispersystems.libsignal.util.Medium;
38 import org.whispersystems.libsignal.util.guava.Optional;
39 import org.whispersystems.signalservice.api.SignalServiceAccountManager;
40 import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
41 import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
42 import org.whispersystems.signalservice.api.SignalServiceMessageSender;
43 import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
44 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
45 import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
46 import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
47 import org.whispersystems.signalservice.api.messages.*;
48 import org.whispersystems.signalservice.api.messages.multidevice.*;
49 import org.whispersystems.signalservice.api.push.ContactTokenDetails;
50 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
51 import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
52 import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
53 import org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException;
54 import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
55 import org.whispersystems.signalservice.api.util.InvalidNumberException;
56 import org.whispersystems.signalservice.api.util.SleepTimer;
57 import org.whispersystems.signalservice.api.util.UptimeSleepTimer;
58 import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
59 import org.whispersystems.signalservice.internal.util.Base64;
60
61 import java.io.*;
62 import java.net.URI;
63 import java.nio.file.Files;
64 import java.nio.file.Paths;
65 import java.nio.file.StandardCopyOption;
66 import java.util.*;
67 import java.util.concurrent.TimeUnit;
68 import java.util.concurrent.TimeoutException;
69
70 public class Manager implements Signal {
71
72 private final String settingsPath;
73 private final String dataPath;
74 private final String attachmentsPath;
75 private final String avatarsPath;
76 private final SleepTimer timer = new UptimeSleepTimer();
77
78 private SignalAccount account;
79 private String username;
80 private SignalServiceAccountManager accountManager;
81 private SignalServiceMessagePipe messagePipe = null;
82 private SignalServiceMessagePipe unidentifiedMessagePipe = null;
83
84 public Manager(String username, String settingsPath) {
85 this.username = username;
86 this.settingsPath = settingsPath;
87 this.dataPath = this.settingsPath + "/data";
88 this.attachmentsPath = this.settingsPath + "/attachments";
89 this.avatarsPath = this.settingsPath + "/avatars";
90
91 }
92
93 public String getUsername() {
94 return username;
95 }
96
97 private IdentityKey getIdentity() {
98 return account.getSignalProtocolStore().getIdentityKeyPair().getPublicKey();
99 }
100
101 public int getDeviceId() {
102 return account.getDeviceId();
103 }
104
105 private String getMessageCachePath() {
106 return this.dataPath + "/" + username + ".d/msg-cache";
107 }
108
109 private String getMessageCachePath(String sender) {
110 return getMessageCachePath() + "/" + sender.replace("/", "_");
111 }
112
113 private File getMessageCacheFile(String sender, long now, long timestamp) throws IOException {
114 String cachePath = getMessageCachePath(sender);
115 IOUtils.createPrivateDirectories(cachePath);
116 return new File(cachePath + "/" + now + "_" + timestamp);
117 }
118
119 public boolean userHasKeys() {
120 return account != null && account.getSignalProtocolStore() != null;
121 }
122
123 public void init() throws IOException {
124 if (!SignalAccount.userExists(dataPath, username)) {
125 return;
126 }
127 account = SignalAccount.load(dataPath, username);
128
129 migrateLegacyConfigs();
130
131 accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, username, account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer);
132 try {
133 if (account.isRegistered() && accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) {
134 refreshPreKeys();
135 account.save();
136 }
137 } catch (AuthorizationFailedException e) {
138 System.err.println("Authorization failed, was the number registered elsewhere?");
139 }
140 }
141
142 private void migrateLegacyConfigs() {
143 // Copy group avatars that were previously stored in the attachments folder
144 // to the new avatar folder
145 if (JsonGroupStore.groupsWithLegacyAvatarId.size() > 0) {
146 for (GroupInfo g : JsonGroupStore.groupsWithLegacyAvatarId) {
147 File avatarFile = getGroupAvatarFile(g.groupId);
148 File attachmentFile = getAttachmentFile(g.getAvatarId());
149 if (!avatarFile.exists() && attachmentFile.exists()) {
150 try {
151 IOUtils.createPrivateDirectories(avatarsPath);
152 Files.copy(attachmentFile.toPath(), avatarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
153 } catch (Exception e) {
154 // Ignore
155 }
156 }
157 }
158 JsonGroupStore.groupsWithLegacyAvatarId.clear();
159 account.save();
160 }
161 if (account.getProfileKey() == null) {
162 // Old config file, creating new profile key
163 account.setProfileKey(KeyUtils.createProfileKey());
164 }
165 }
166
167 private void createNewIdentity() throws IOException {
168 IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
169 int registrationId = KeyHelper.generateRegistrationId(false);
170 if (username == null) {
171 account = SignalAccount.createTemporaryAccount(identityKey, registrationId);
172 } else {
173 byte[] profileKey = KeyUtils.createProfileKey();
174 account = SignalAccount.create(dataPath, username, identityKey, registrationId, profileKey);
175 account.save();
176 }
177 }
178
179 public boolean isRegistered() {
180 return account != null && account.isRegistered();
181 }
182
183 public void register(boolean voiceVerification) throws IOException {
184 if (account == null) {
185 createNewIdentity();
186 }
187 account.setPassword(KeyUtils.createPassword());
188 accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, account.getUsername(), account.getPassword(), BaseConfig.USER_AGENT, timer);
189
190 if (voiceVerification)
191 accountManager.requestVoiceVerificationCode();
192 else
193 accountManager.requestSmsVerificationCode();
194
195 account.setRegistered(false);
196 account.save();
197 }
198
199 public void updateAccountAttributes() throws IOException {
200 accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), getSelfUnidentifiedAccessKey(), false);
201 }
202
203 public void unregister() throws IOException {
204 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
205 // If this is the master device, other users can't send messages to this number anymore.
206 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
207 accountManager.setGcmId(Optional.<String>absent());
208 }
209
210 public String getDeviceLinkUri() throws TimeoutException, IOException {
211 if (account == null) {
212 createNewIdentity();
213 }
214 account.setPassword(KeyUtils.createPassword());
215 accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, username, account.getPassword(), BaseConfig.USER_AGENT, timer);
216 String uuid = accountManager.getNewDeviceUuid();
217
218 return Utils.createDeviceLinkUri(new Utils.DeviceLinkInfo(uuid, getIdentity().getPublicKey()));
219 }
220
221 public void finishDeviceLink(String deviceName) throws IOException, InvalidKeyException, TimeoutException, UserAlreadyExists {
222 account.setSignalingKey(KeyUtils.createSignalingKey());
223 SignalServiceAccountManager.NewDeviceRegistrationReturn ret = accountManager.finishNewDeviceRegistration(account.getSignalProtocolStore().getIdentityKeyPair(), account.getSignalingKey(), false, true, account.getSignalProtocolStore().getLocalRegistrationId(), deviceName);
224
225 username = ret.getNumber();
226 // TODO do this check before actually registering
227 if (SignalAccount.userExists(dataPath, username)) {
228 throw new UserAlreadyExists(username, SignalAccount.getFileName(dataPath, username));
229 }
230
231 // Create new account with the synced identity
232 byte[] profileKey = ret.getProfileKey();
233 if (profileKey == null) {
234 profileKey = KeyUtils.createProfileKey();
235 }
236 account = SignalAccount.createLinkedAccount(dataPath, username, account.getPassword(), ret.getDeviceId(), ret.getIdentity(), account.getSignalProtocolStore().getLocalRegistrationId(), account.getSignalingKey(), profileKey);
237
238 refreshPreKeys();
239
240 requestSyncGroups();
241 requestSyncContacts();
242 requestSyncBlocked();
243 requestSyncConfiguration();
244
245 account.save();
246 }
247
248 public List<DeviceInfo> getLinkedDevices() throws IOException {
249 List<DeviceInfo> devices = accountManager.getDevices();
250 account.setMultiDevice(devices.size() > 1);
251 account.save();
252 return devices;
253 }
254
255 public void removeLinkedDevices(int deviceId) throws IOException {
256 accountManager.removeDevice(deviceId);
257 List<DeviceInfo> devices = accountManager.getDevices();
258 account.setMultiDevice(devices.size() > 1);
259 account.save();
260 }
261
262 public void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException {
263 Utils.DeviceLinkInfo info = Utils.parseDeviceLinkUri(linkUri);
264
265 addDevice(info.deviceIdentifier, info.deviceKey);
266 }
267
268 private void addDevice(String deviceIdentifier, ECPublicKey deviceKey) throws IOException, InvalidKeyException {
269 IdentityKeyPair identityKeyPair = account.getSignalProtocolStore().getIdentityKeyPair();
270 String verificationCode = accountManager.getNewDeviceVerificationCode();
271
272 accountManager.addDevice(deviceIdentifier, deviceKey, identityKeyPair, Optional.of(account.getProfileKey()), verificationCode);
273 account.setMultiDevice(true);
274 account.save();
275 }
276
277 private List<PreKeyRecord> generatePreKeys() {
278 List<PreKeyRecord> records = new ArrayList<>(BaseConfig.PREKEY_BATCH_SIZE);
279
280 final int offset = account.getPreKeyIdOffset();
281 for (int i = 0; i < BaseConfig.PREKEY_BATCH_SIZE; i++) {
282 int preKeyId = (offset + i) % Medium.MAX_VALUE;
283 ECKeyPair keyPair = Curve.generateKeyPair();
284 PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
285
286 records.add(record);
287 }
288
289 account.addPreKeys(records);
290 account.save();
291
292 return records;
293 }
294
295 private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) {
296 try {
297 ECKeyPair keyPair = Curve.generateKeyPair();
298 byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
299 SignedPreKeyRecord record = new SignedPreKeyRecord(account.getNextSignedPreKeyId(), System.currentTimeMillis(), keyPair, signature);
300
301 account.addSignedPreKey(record);
302 account.save();
303
304 return record;
305 } catch (InvalidKeyException e) {
306 throw new AssertionError(e);
307 }
308 }
309
310 public void verifyAccount(String verificationCode, String pin) throws IOException {
311 verificationCode = verificationCode.replace("-", "");
312 account.setSignalingKey(KeyUtils.createSignalingKey());
313 // TODO make unrestricted unidentified access configurable
314 accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, getSelfUnidentifiedAccessKey(), false);
315
316 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
317 account.setRegistered(true);
318 account.setRegistrationLockPin(pin);
319
320 refreshPreKeys();
321 account.save();
322 }
323
324 public void setRegistrationLockPin(Optional<String> pin) throws IOException {
325 accountManager.setPin(pin);
326 if (pin.isPresent()) {
327 account.setRegistrationLockPin(pin.get());
328 } else {
329 account.setRegistrationLockPin(null);
330 }
331 account.save();
332 }
333
334 private void refreshPreKeys() throws IOException {
335 List<PreKeyRecord> oneTimePreKeys = generatePreKeys();
336 final IdentityKeyPair identityKeyPair = account.getSignalProtocolStore().getIdentityKeyPair();
337 SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(identityKeyPair);
338
339 accountManager.setPreKeys(getIdentity(), signedPreKeyRecord, oneTimePreKeys);
340 }
341
342 private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException {
343 File file = getGroupAvatarFile(groupId);
344 if (!file.exists()) {
345 return Optional.absent();
346 }
347
348 return Optional.of(Utils.createAttachment(file));
349 }
350
351 private Optional<SignalServiceAttachmentStream> createContactAvatarAttachment(String number) throws IOException {
352 File file = getContactAvatarFile(number);
353 if (!file.exists()) {
354 return Optional.absent();
355 }
356
357 return Optional.of(Utils.createAttachment(file));
358 }
359
360 private GroupInfo getGroupForSending(byte[] groupId) throws GroupNotFoundException, NotAGroupMemberException {
361 GroupInfo g = account.getGroupStore().getGroup(groupId);
362 if (g == null) {
363 throw new GroupNotFoundException(groupId);
364 }
365 for (String member : g.members) {
366 if (member.equals(this.username)) {
367 return g;
368 }
369 }
370 throw new NotAGroupMemberException(groupId, g.name);
371 }
372
373 public List<GroupInfo> getGroups() {
374 return account.getGroupStore().getGroups();
375 }
376
377 @Override
378 public void sendGroupMessage(String messageText, List<String> attachments,
379 byte[] groupId)
380 throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
381 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
382 if (attachments != null) {
383 messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
384 }
385 if (groupId != null) {
386 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
387 .withId(groupId)
388 .build();
389 messageBuilder.asGroupMessage(group);
390 }
391 ThreadInfo thread = account.getThreadStore().getThread(Base64.encodeBytes(groupId));
392 if (thread != null) {
393 messageBuilder.withExpiration(thread.messageExpirationTime);
394 }
395
396 final GroupInfo g = getGroupForSending(groupId);
397
398 // Don't send group message to ourself
399 final List<String> membersSend = new ArrayList<>(g.members);
400 membersSend.remove(this.username);
401 sendMessageLegacy(messageBuilder, membersSend);
402 }
403
404 public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions {
405 SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
406 .withId(groupId)
407 .build();
408
409 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
410 .asGroupMessage(group);
411
412 final GroupInfo g = getGroupForSending(groupId);
413 g.members.remove(this.username);
414 account.getGroupStore().updateGroup(g);
415
416 sendMessageLegacy(messageBuilder, g.members);
417 }
418
419 private byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<String> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
420 GroupInfo g;
421 if (groupId == null) {
422 // Create new group
423 g = new GroupInfo(KeyUtils.createGroupId());
424 g.members.add(username);
425 } else {
426 g = getGroupForSending(groupId);
427 }
428
429 if (name != null) {
430 g.name = name;
431 }
432
433 if (members != null) {
434 Set<String> newMembers = new HashSet<>();
435 for (String member : members) {
436 try {
437 member = Utils.canonicalizeNumber(member, username);
438 } catch (InvalidNumberException e) {
439 System.err.println("Failed to add member \"" + member + "\" to group: " + e.getMessage());
440 System.err.println("Aborting…");
441 System.exit(1);
442 }
443 if (g.members.contains(member)) {
444 continue;
445 }
446 newMembers.add(member);
447 g.members.add(member);
448 }
449 final List<ContactTokenDetails> contacts = accountManager.getContacts(newMembers);
450 if (contacts.size() != newMembers.size()) {
451 // Some of the new members are not registered on Signal
452 for (ContactTokenDetails contact : contacts) {
453 newMembers.remove(contact.getNumber());
454 }
455 System.err.println("Failed to add members " + Util.join(", ", newMembers) + " to group: Not registered on Signal");
456 System.err.println("Aborting…");
457 System.exit(1);
458 }
459 }
460
461 if (avatarFile != null) {
462 IOUtils.createPrivateDirectories(avatarsPath);
463 File aFile = getGroupAvatarFile(g.groupId);
464 Files.copy(Paths.get(avatarFile), aFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
465 }
466
467 account.getGroupStore().updateGroup(g);
468
469 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
470
471 // Don't send group message to ourself
472 final List<String> membersSend = new ArrayList<>(g.members);
473 membersSend.remove(this.username);
474 sendMessageLegacy(messageBuilder, membersSend);
475 return g.groupId;
476 }
477
478 private void sendUpdateGroupMessage(byte[] groupId, String recipient) throws IOException, EncapsulatedExceptions {
479 if (groupId == null) {
480 return;
481 }
482 GroupInfo g = getGroupForSending(groupId);
483
484 if (!g.members.contains(recipient)) {
485 return;
486 }
487
488 SignalServiceDataMessage.Builder messageBuilder = getGroupUpdateMessageBuilder(g);
489
490 // Send group message only to the recipient who requested it
491 final List<String> membersSend = new ArrayList<>();
492 membersSend.add(recipient);
493 sendMessageLegacy(messageBuilder, membersSend);
494 }
495
496 private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) {
497 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
498 .withId(g.groupId)
499 .withName(g.name)
500 .withMembers(new ArrayList<>(g.members));
501
502 File aFile = getGroupAvatarFile(g.groupId);
503 if (aFile.exists()) {
504 try {
505 group.withAvatar(Utils.createAttachment(aFile));
506 } catch (IOException e) {
507 throw new AttachmentInvalidException(aFile.toString(), e);
508 }
509 }
510
511 return SignalServiceDataMessage.newBuilder()
512 .asGroupMessage(group.build());
513 }
514
515 private void sendGroupInfoRequest(byte[] groupId, String recipient) throws IOException, EncapsulatedExceptions {
516 if (groupId == null) {
517 return;
518 }
519
520 SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.REQUEST_INFO)
521 .withId(groupId);
522
523 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
524 .asGroupMessage(group.build());
525
526 // Send group info request message to the recipient who sent us a message with this groupId
527 final List<String> membersSend = new ArrayList<>();
528 membersSend.add(recipient);
529 sendMessageLegacy(messageBuilder, membersSend);
530 }
531
532 @Override
533 public void sendMessage(String message, List<String> attachments, String recipient)
534 throws EncapsulatedExceptions, AttachmentInvalidException, IOException {
535 List<String> recipients = new ArrayList<>(1);
536 recipients.add(recipient);
537 sendMessage(message, attachments, recipients);
538 }
539
540 @Override
541 public void sendMessage(String messageText, List<String> attachments,
542 List<String> recipients)
543 throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
544 final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
545 if (attachments != null) {
546 messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
547 }
548 sendMessageLegacy(messageBuilder, recipients);
549 }
550
551 @Override
552 public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions {
553 SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
554 .asEndSessionMessage();
555
556 sendMessageLegacy(messageBuilder, recipients);
557 }
558
559 @Override
560 public String getContactName(String number) {
561 ContactInfo contact = account.getContactStore().getContact(number);
562 if (contact == null) {
563 return "";
564 } else {
565 return contact.name;
566 }
567 }
568
569 @Override
570 public void setContactName(String number, String name) {
571 ContactInfo contact = account.getContactStore().getContact(number);
572 if (contact == null) {
573 contact = new ContactInfo();
574 contact.number = number;
575 System.err.println("Add contact " + number + " named " + name);
576 } else {
577 System.err.println("Updating contact " + number + " name " + contact.name + " -> " + name);
578 }
579 contact.name = name;
580 account.getContactStore().updateContact(contact);
581 account.save();
582 }
583
584 @Override
585 public List<byte[]> getGroupIds() {
586 List<GroupInfo> groups = getGroups();
587 List<byte[]> ids = new ArrayList<>(groups.size());
588 for (GroupInfo group : groups) {
589 ids.add(group.groupId);
590 }
591 return ids;
592 }
593
594 @Override
595 public String getGroupName(byte[] groupId) {
596 GroupInfo group = getGroup(groupId);
597 if (group == null) {
598 return "";
599 } else {
600 return group.name;
601 }
602 }
603
604 @Override
605 public List<String> getGroupMembers(byte[] groupId) {
606 GroupInfo group = getGroup(groupId);
607 if (group == null) {
608 return new ArrayList<>();
609 } else {
610 return new ArrayList<>(group.members);
611 }
612 }
613
614 @Override
615 public byte[] updateGroup(byte[] groupId, String name, List<String> members, String avatar) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
616 if (groupId.length == 0) {
617 groupId = null;
618 }
619 if (name.isEmpty()) {
620 name = null;
621 }
622 if (members.size() == 0) {
623 members = null;
624 }
625 if (avatar.isEmpty()) {
626 avatar = null;
627 }
628 return sendUpdateGroupMessage(groupId, name, members, avatar);
629 }
630
631 /**
632 * Change the expiration timer for a thread (number of groupId)
633 *
634 * @param numberOrGroupId
635 * @param messageExpirationTimer
636 */
637 public void setExpirationTimer(String numberOrGroupId, int messageExpirationTimer) {
638 ThreadInfo thread = account.getThreadStore().getThread(numberOrGroupId);
639 thread.messageExpirationTime = messageExpirationTimer;
640 account.getThreadStore().updateThread(thread);
641 }
642
643 private void requestSyncGroups() throws IOException {
644 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.GROUPS).build();
645 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
646 try {
647 sendSyncMessage(message);
648 } catch (UntrustedIdentityException e) {
649 e.printStackTrace();
650 }
651 }
652
653 private void requestSyncContacts() throws IOException {
654 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONTACTS).build();
655 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
656 try {
657 sendSyncMessage(message);
658 } catch (UntrustedIdentityException e) {
659 e.printStackTrace();
660 }
661 }
662
663 private void requestSyncBlocked() throws IOException {
664 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.BLOCKED).build();
665 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
666 try {
667 sendSyncMessage(message);
668 } catch (UntrustedIdentityException e) {
669 e.printStackTrace();
670 }
671 }
672
673 private void requestSyncConfiguration() throws IOException {
674 SignalServiceProtos.SyncMessage.Request r = SignalServiceProtos.SyncMessage.Request.newBuilder().setType(SignalServiceProtos.SyncMessage.Request.Type.CONFIGURATION).build();
675 SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
676 try {
677 sendSyncMessage(message);
678 } catch (UntrustedIdentityException e) {
679 e.printStackTrace();
680 }
681 }
682
683 private byte[] getSelfUnidentifiedAccessKey() {
684 return UnidentifiedAccess.deriveAccessKeyFrom(account.getProfileKey());
685 }
686
687 private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) {
688 // TODO implement
689 return null;
690 }
691
692 private Optional<UnidentifiedAccessPair> getAccessForSync() {
693 // TODO implement
694 return Optional.absent();
695 }
696
697 private List<Optional<UnidentifiedAccessPair>> getAccessFor(Collection<SignalServiceAddress> recipients) {
698 List<Optional<UnidentifiedAccessPair>> result = new ArrayList<>(recipients.size());
699 for (SignalServiceAddress recipient : recipients) {
700 result.add(Optional.<UnidentifiedAccessPair>absent());
701 }
702 return result;
703 }
704
705 private Optional<UnidentifiedAccessPair> getAccessFor(SignalServiceAddress recipient) {
706 // TODO implement
707 return Optional.absent();
708 }
709
710 private void sendSyncMessage(SignalServiceSyncMessage message)
711 throws IOException, UntrustedIdentityException {
712 SignalServiceMessageSender messageSender = new SignalServiceMessageSender(BaseConfig.serviceConfiguration, username, account.getPassword(),
713 account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
714 try {
715 messageSender.sendMessage(message, getAccessForSync());
716 } catch (UntrustedIdentityException e) {
717 account.getSignalProtocolStore().saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
718 throw e;
719 }
720 }
721
722 /**
723 * This method throws an EncapsulatedExceptions exception instead of returning a list of SendMessageResult.
724 */
725 private void sendMessageLegacy(SignalServiceDataMessage.Builder messageBuilder, Collection<String> recipients)
726 throws EncapsulatedExceptions, IOException {
727 List<SendMessageResult> results = sendMessage(messageBuilder, recipients);
728
729 List<UntrustedIdentityException> untrustedIdentities = new LinkedList<>();
730 List<UnregisteredUserException> unregisteredUsers = new LinkedList<>();
731 List<NetworkFailureException> networkExceptions = new LinkedList<>();
732
733 for (SendMessageResult result : results) {
734 if (result.isUnregisteredFailure()) {
735 unregisteredUsers.add(new UnregisteredUserException(result.getAddress().getNumber(), null));
736 } else if (result.isNetworkFailure()) {
737 networkExceptions.add(new NetworkFailureException(result.getAddress().getNumber(), null));
738 } else if (result.getIdentityFailure() != null) {
739 untrustedIdentities.add(new UntrustedIdentityException("Untrusted", result.getAddress().getNumber(), result.getIdentityFailure().getIdentityKey()));
740 }
741 }
742 if (!untrustedIdentities.isEmpty() || !unregisteredUsers.isEmpty() || !networkExceptions.isEmpty()) {
743 throw new EncapsulatedExceptions(untrustedIdentities, unregisteredUsers, networkExceptions);
744 }
745 }
746
747 private List<SendMessageResult> sendMessage(SignalServiceDataMessage.Builder messageBuilder, Collection<String> recipients)
748 throws IOException {
749 Set<SignalServiceAddress> recipientsTS = Utils.getSignalServiceAddresses(recipients, username);
750 if (recipientsTS == null) {
751 account.save();
752 return Collections.emptyList();
753 }
754
755 SignalServiceDataMessage message = null;
756 try {
757 SignalServiceMessageSender messageSender = new SignalServiceMessageSender(BaseConfig.serviceConfiguration, username, account.getPassword(),
758 account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.<SignalServiceMessageSender.EventListener>absent());
759
760 message = messageBuilder.build();
761 if (message.getGroupInfo().isPresent()) {
762 try {
763 List<SendMessageResult> result = messageSender.sendMessage(new ArrayList<>(recipientsTS), getAccessFor(recipientsTS), message);
764 for (SendMessageResult r : result) {
765 if (r.getIdentityFailure() != null) {
766 account.getSignalProtocolStore().saveIdentity(r.getAddress().getNumber(), r.getIdentityFailure().getIdentityKey(), TrustLevel.UNTRUSTED);
767 }
768 }
769 return result;
770 } catch (UntrustedIdentityException e) {
771 account.getSignalProtocolStore().saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
772 return Collections.emptyList();
773 }
774 } else {
775 // Send to all individually, so sync messages are sent correctly
776 List<SendMessageResult> results = new ArrayList<>(recipientsTS.size());
777 for (SignalServiceAddress address : recipientsTS) {
778 ThreadInfo thread = account.getThreadStore().getThread(address.getNumber());
779 if (thread != null) {
780 messageBuilder.withExpiration(thread.messageExpirationTime);
781 } else {
782 messageBuilder.withExpiration(0);
783 }
784 message = messageBuilder.build();
785 try {
786 SendMessageResult result = messageSender.sendMessage(address, getAccessFor(address), message);
787 results.add(result);
788 } catch (UntrustedIdentityException e) {
789 account.getSignalProtocolStore().saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
790 results.add(SendMessageResult.identityFailure(address, e.getIdentityKey()));
791 }
792 }
793 return results;
794 }
795 } finally {
796 if (message != null && message.isEndSession()) {
797 for (SignalServiceAddress recipient : recipientsTS) {
798 handleEndSession(recipient.getNumber());
799 }
800 }
801 account.save();
802 }
803 }
804
805 private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, ProtocolUntrustedIdentityException, SelfSendException {
806 SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(username), account.getSignalProtocolStore(), Utils.getCertificateValidator());
807 try {
808 return cipher.decrypt(envelope);
809 } catch (ProtocolUntrustedIdentityException e) {
810 // TODO We don't get the new untrusted identity from ProtocolUntrustedIdentityException anymore ... we need to get it from somewhere else
811 // account.getSignalProtocolStore().saveIdentity(e.getSender(), e.getUntrustedIdentity(), TrustLevel.UNTRUSTED);
812 throw e;
813 }
814 }
815
816 private void handleEndSession(String source) {
817 account.getSignalProtocolStore().deleteAllSessions(source);
818 }
819
820 private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination, boolean ignoreAttachments) {
821 String threadId;
822 if (message.getGroupInfo().isPresent()) {
823 SignalServiceGroup groupInfo = message.getGroupInfo().get();
824 threadId = Base64.encodeBytes(groupInfo.getGroupId());
825 GroupInfo group = account.getGroupStore().getGroup(groupInfo.getGroupId());
826 switch (groupInfo.getType()) {
827 case UPDATE:
828 if (group == null) {
829 group = new GroupInfo(groupInfo.getGroupId());
830 }
831
832 if (groupInfo.getAvatar().isPresent()) {
833 SignalServiceAttachment avatar = groupInfo.getAvatar().get();
834 if (avatar.isPointer()) {
835 try {
836 retrieveGroupAvatarAttachment(avatar.asPointer(), group.groupId);
837 } catch (IOException | InvalidMessageException e) {
838 System.err.println("Failed to retrieve group avatar (" + avatar.asPointer().getId() + "): " + e.getMessage());
839 }
840 }
841 }
842
843 if (groupInfo.getName().isPresent()) {
844 group.name = groupInfo.getName().get();
845 }
846
847 if (groupInfo.getMembers().isPresent()) {
848 group.members.addAll(groupInfo.getMembers().get());
849 }
850
851 account.getGroupStore().updateGroup(group);
852 break;
853 case DELIVER:
854 if (group == null) {
855 try {
856 sendGroupInfoRequest(groupInfo.getGroupId(), source);
857 } catch (IOException | EncapsulatedExceptions e) {
858 e.printStackTrace();
859 }
860 }
861 break;
862 case QUIT:
863 if (group == null) {
864 try {
865 sendGroupInfoRequest(groupInfo.getGroupId(), source);
866 } catch (IOException | EncapsulatedExceptions e) {
867 e.printStackTrace();
868 }
869 } else {
870 group.members.remove(source);
871 account.getGroupStore().updateGroup(group);
872 }
873 break;
874 case REQUEST_INFO:
875 if (group != null) {
876 try {
877 sendUpdateGroupMessage(groupInfo.getGroupId(), source);
878 } catch (IOException | EncapsulatedExceptions e) {
879 e.printStackTrace();
880 } catch (NotAGroupMemberException e) {
881 // We have left this group, so don't send a group update message
882 }
883 }
884 break;
885 }
886 } else {
887 if (isSync) {
888 threadId = destination;
889 } else {
890 threadId = source;
891 }
892 }
893 if (message.isEndSession()) {
894 handleEndSession(isSync ? destination : source);
895 }
896 if (message.isExpirationUpdate() || message.getBody().isPresent()) {
897 ThreadInfo thread = account.getThreadStore().getThread(threadId);
898 if (thread == null) {
899 thread = new ThreadInfo();
900 thread.id = threadId;
901 }
902 if (thread.messageExpirationTime != message.getExpiresInSeconds()) {
903 thread.messageExpirationTime = message.getExpiresInSeconds();
904 account.getThreadStore().updateThread(thread);
905 }
906 }
907 if (message.getAttachments().isPresent() && !ignoreAttachments) {
908 for (SignalServiceAttachment attachment : message.getAttachments().get()) {
909 if (attachment.isPointer()) {
910 try {
911 retrieveAttachment(attachment.asPointer());
912 } catch (IOException | InvalidMessageException e) {
913 System.err.println("Failed to retrieve attachment (" + attachment.asPointer().getId() + "): " + e.getMessage());
914 }
915 }
916 }
917 }
918 if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
919 if (source.equals(username)) {
920 this.account.setProfileKey(message.getProfileKey().get());
921 }
922 ContactInfo contact = account.getContactStore().getContact(source);
923 if (contact == null) {
924 contact = new ContactInfo();
925 contact.number = source;
926 }
927 contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
928 }
929 }
930
931 private void retryFailedReceivedMessages(ReceiveMessageHandler handler, boolean ignoreAttachments) {
932 final File cachePath = new File(getMessageCachePath());
933 if (!cachePath.exists()) {
934 return;
935 }
936 for (final File dir : Objects.requireNonNull(cachePath.listFiles())) {
937 if (!dir.isDirectory()) {
938 continue;
939 }
940
941 for (final File fileEntry : Objects.requireNonNull(dir.listFiles())) {
942 if (!fileEntry.isFile()) {
943 continue;
944 }
945 SignalServiceEnvelope envelope;
946 try {
947 envelope = Utils.loadEnvelope(fileEntry);
948 if (envelope == null) {
949 continue;
950 }
951 } catch (IOException e) {
952 e.printStackTrace();
953 continue;
954 }
955 SignalServiceContent content = null;
956 if (!envelope.isReceipt()) {
957 try {
958 content = decryptMessage(envelope);
959 } catch (Exception e) {
960 continue;
961 }
962 handleMessage(envelope, content, ignoreAttachments);
963 }
964 account.save();
965 handler.handleMessage(envelope, content, null);
966 try {
967 Files.delete(fileEntry.toPath());
968 } catch (IOException e) {
969 System.err.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage());
970 }
971 }
972 // Try to delete directory if empty
973 dir.delete();
974 }
975 }
976
977 public void receiveMessages(long timeout, TimeUnit unit, boolean returnOnTimeout, boolean ignoreAttachments, ReceiveMessageHandler handler) throws IOException {
978 retryFailedReceivedMessages(handler, ignoreAttachments);
979 final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, username, account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer);
980
981 try {
982 if (messagePipe == null) {
983 messagePipe = messageReceiver.createMessagePipe();
984 }
985
986 while (true) {
987 SignalServiceEnvelope envelope;
988 SignalServiceContent content = null;
989 Exception exception = null;
990 final long now = new Date().getTime();
991 try {
992 envelope = messagePipe.read(timeout, unit, new SignalServiceMessagePipe.MessagePipeCallback() {
993 @Override
994 public void onMessage(SignalServiceEnvelope envelope) {
995 // store message on disk, before acknowledging receipt to the server
996 try {
997 File cacheFile = getMessageCacheFile(envelope.getSource(), now, envelope.getTimestamp());
998 Utils.storeEnvelope(envelope, cacheFile);
999 } catch (IOException e) {
1000 System.err.println("Failed to store encrypted message in disk cache, ignoring: " + e.getMessage());
1001 }
1002 }
1003 });
1004 } catch (TimeoutException e) {
1005 if (returnOnTimeout)
1006 return;
1007 continue;
1008 } catch (InvalidVersionException e) {
1009 System.err.println("Ignoring error: " + e.getMessage());
1010 continue;
1011 }
1012 if (!envelope.isReceipt()) {
1013 try {
1014 content = decryptMessage(envelope);
1015 } catch (Exception e) {
1016 exception = e;
1017 }
1018 handleMessage(envelope, content, ignoreAttachments);
1019 }
1020 account.save();
1021 handler.handleMessage(envelope, content, exception);
1022 if (!(exception instanceof ProtocolUntrustedIdentityException)) {
1023 File cacheFile = null;
1024 try {
1025 cacheFile = getMessageCacheFile(envelope.getSource(), now, envelope.getTimestamp());
1026 Files.delete(cacheFile.toPath());
1027 // Try to delete directory if empty
1028 new File(getMessageCachePath()).delete();
1029 } catch (IOException e) {
1030 System.err.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage());
1031 }
1032 }
1033 }
1034 } finally {
1035 if (messagePipe != null) {
1036 messagePipe.shutdown();
1037 messagePipe = null;
1038 }
1039 }
1040 }
1041
1042 private void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) {
1043 if (content != null) {
1044 if (content.getDataMessage().isPresent()) {
1045 SignalServiceDataMessage message = content.getDataMessage().get();
1046 handleSignalServiceDataMessage(message, false, envelope.getSource(), username, ignoreAttachments);
1047 }
1048 if (content.getSyncMessage().isPresent()) {
1049 account.setMultiDevice(true);
1050 SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
1051 if (syncMessage.getSent().isPresent()) {
1052 SignalServiceDataMessage message = syncMessage.getSent().get().getMessage();
1053 handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get(), ignoreAttachments);
1054 }
1055 if (syncMessage.getRequest().isPresent()) {
1056 RequestMessage rm = syncMessage.getRequest().get();
1057 if (rm.isContactsRequest()) {
1058 try {
1059 sendContacts();
1060 } catch (UntrustedIdentityException | IOException e) {
1061 e.printStackTrace();
1062 }
1063 }
1064 if (rm.isGroupsRequest()) {
1065 try {
1066 sendGroups();
1067 } catch (UntrustedIdentityException | IOException e) {
1068 e.printStackTrace();
1069 }
1070 }
1071 // TODO Handle rm.isBlockedListRequest(); rm.isConfigurationRequest();
1072 }
1073 if (syncMessage.getGroups().isPresent()) {
1074 File tmpFile = null;
1075 try {
1076 tmpFile = IOUtils.createTempFile();
1077 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile)) {
1078 DeviceGroupsInputStream s = new DeviceGroupsInputStream(attachmentAsStream);
1079 DeviceGroup g;
1080 while ((g = s.read()) != null) {
1081 GroupInfo syncGroup = account.getGroupStore().getGroup(g.getId());
1082 if (syncGroup == null) {
1083 syncGroup = new GroupInfo(g.getId());
1084 }
1085 if (g.getName().isPresent()) {
1086 syncGroup.name = g.getName().get();
1087 }
1088 syncGroup.members.addAll(g.getMembers());
1089 syncGroup.active = g.isActive();
1090 if (g.getColor().isPresent()) {
1091 syncGroup.color = g.getColor().get();
1092 }
1093
1094 if (g.getAvatar().isPresent()) {
1095 retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId);
1096 }
1097 account.getGroupStore().updateGroup(syncGroup);
1098 }
1099 }
1100 } catch (Exception e) {
1101 e.printStackTrace();
1102 } finally {
1103 if (tmpFile != null) {
1104 try {
1105 Files.delete(tmpFile.toPath());
1106 } catch (IOException e) {
1107 System.err.println("Failed to delete received groups temp file “" + tmpFile + "”: " + e.getMessage());
1108 }
1109 }
1110 }
1111 }
1112 if (syncMessage.getBlockedList().isPresent()) {
1113 // TODO store list of blocked numbers
1114 }
1115 if (syncMessage.getContacts().isPresent()) {
1116 File tmpFile = null;
1117 try {
1118 tmpFile = IOUtils.createTempFile();
1119 final ContactsMessage contactsMessage = syncMessage.getContacts().get();
1120 try (InputStream attachmentAsStream = retrieveAttachmentAsStream(contactsMessage.getContactsStream().asPointer(), tmpFile)) {
1121 DeviceContactsInputStream s = new DeviceContactsInputStream(attachmentAsStream);
1122 if (contactsMessage.isComplete()) {
1123 account.getContactStore().clear();
1124 }
1125 DeviceContact c;
1126 while ((c = s.read()) != null) {
1127 if (c.getNumber().equals(account.getUsername()) && c.getProfileKey().isPresent()) {
1128 account.setProfileKey(c.getProfileKey().get());
1129 }
1130 ContactInfo contact = account.getContactStore().getContact(c.getNumber());
1131 if (contact == null) {
1132 contact = new ContactInfo();
1133 contact.number = c.getNumber();
1134 }
1135 if (c.getName().isPresent()) {
1136 contact.name = c.getName().get();
1137 }
1138 if (c.getColor().isPresent()) {
1139 contact.color = c.getColor().get();
1140 }
1141 if (c.getProfileKey().isPresent()) {
1142 contact.profileKey = Base64.encodeBytes(c.getProfileKey().get());
1143 }
1144 if (c.getVerified().isPresent()) {
1145 final VerifiedMessage verifiedMessage = c.getVerified().get();
1146 account.getSignalProtocolStore().saveIdentity(verifiedMessage.getDestination(), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
1147 }
1148 if (c.getExpirationTimer().isPresent()) {
1149 ThreadInfo thread = account.getThreadStore().getThread(c.getNumber());
1150 thread.messageExpirationTime = c.getExpirationTimer().get();
1151 account.getThreadStore().updateThread(thread);
1152 }
1153 if (c.isBlocked()) {
1154 // TODO store list of blocked numbers
1155 }
1156 account.getContactStore().updateContact(contact);
1157
1158 if (c.getAvatar().isPresent()) {
1159 retrieveContactAvatarAttachment(c.getAvatar().get(), contact.number);
1160 }
1161 }
1162 }
1163 } catch (Exception e) {
1164 e.printStackTrace();
1165 } finally {
1166 if (tmpFile != null) {
1167 try {
1168 Files.delete(tmpFile.toPath());
1169 } catch (IOException e) {
1170 System.err.println("Failed to delete received contacts temp file “" + tmpFile + "”: " + e.getMessage());
1171 }
1172 }
1173 }
1174 }
1175 if (syncMessage.getVerified().isPresent()) {
1176 final VerifiedMessage verifiedMessage = syncMessage.getVerified().get();
1177 account.getSignalProtocolStore().saveIdentity(verifiedMessage.getDestination(), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified()));
1178 }
1179 if (syncMessage.getConfiguration().isPresent()) {
1180 // TODO
1181 }
1182 }
1183 }
1184 }
1185
1186 private File getContactAvatarFile(String number) {
1187 return new File(avatarsPath, "contact-" + number);
1188 }
1189
1190 private File retrieveContactAvatarAttachment(SignalServiceAttachment attachment, String number) throws IOException, InvalidMessageException {
1191 IOUtils.createPrivateDirectories(avatarsPath);
1192 if (attachment.isPointer()) {
1193 SignalServiceAttachmentPointer pointer = attachment.asPointer();
1194 return retrieveAttachment(pointer, getContactAvatarFile(number), false);
1195 } else {
1196 SignalServiceAttachmentStream stream = attachment.asStream();
1197 return Utils.retrieveAttachment(stream, getContactAvatarFile(number));
1198 }
1199 }
1200
1201 private File getGroupAvatarFile(byte[] groupId) {
1202 return new File(avatarsPath, "group-" + Base64.encodeBytes(groupId).replace("/", "_"));
1203 }
1204
1205 private File retrieveGroupAvatarAttachment(SignalServiceAttachment attachment, byte[] groupId) throws IOException, InvalidMessageException {
1206 IOUtils.createPrivateDirectories(avatarsPath);
1207 if (attachment.isPointer()) {
1208 SignalServiceAttachmentPointer pointer = attachment.asPointer();
1209 return retrieveAttachment(pointer, getGroupAvatarFile(groupId), false);
1210 } else {
1211 SignalServiceAttachmentStream stream = attachment.asStream();
1212 return Utils.retrieveAttachment(stream, getGroupAvatarFile(groupId));
1213 }
1214 }
1215
1216 public File getAttachmentFile(long attachmentId) {
1217 return new File(attachmentsPath, attachmentId + "");
1218 }
1219
1220 private File retrieveAttachment(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException {
1221 IOUtils.createPrivateDirectories(attachmentsPath);
1222 return retrieveAttachment(pointer, getAttachmentFile(pointer.getId()), true);
1223 }
1224
1225 private File retrieveAttachment(SignalServiceAttachmentPointer pointer, File outputFile, boolean storePreview) throws IOException, InvalidMessageException {
1226 if (storePreview && pointer.getPreview().isPresent()) {
1227 File previewFile = new File(outputFile + ".preview");
1228 try (OutputStream output = new FileOutputStream(previewFile)) {
1229 byte[] preview = pointer.getPreview().get();
1230 output.write(preview, 0, preview.length);
1231 } catch (FileNotFoundException e) {
1232 e.printStackTrace();
1233 return null;
1234 }
1235 }
1236
1237 final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, username, account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer);
1238
1239 File tmpFile = IOUtils.createTempFile();
1240 try (InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile, BaseConfig.MAX_ATTACHMENT_SIZE)) {
1241 try (OutputStream output = new FileOutputStream(outputFile)) {
1242 byte[] buffer = new byte[4096];
1243 int read;
1244
1245 while ((read = input.read(buffer)) != -1) {
1246 output.write(buffer, 0, read);
1247 }
1248 } catch (FileNotFoundException e) {
1249 e.printStackTrace();
1250 return null;
1251 }
1252 } finally {
1253 try {
1254 Files.delete(tmpFile.toPath());
1255 } catch (IOException e) {
1256 System.err.println("Failed to delete received attachment temp file “" + tmpFile + "”: " + e.getMessage());
1257 }
1258 }
1259 return outputFile;
1260 }
1261
1262 private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException {
1263 final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, username, account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer);
1264 return messageReceiver.retrieveAttachment(pointer, tmpFile, BaseConfig.MAX_ATTACHMENT_SIZE);
1265 }
1266
1267 @Override
1268 public boolean isRemote() {
1269 return false;
1270 }
1271
1272 private void sendGroups() throws IOException, UntrustedIdentityException {
1273 File groupsFile = IOUtils.createTempFile();
1274
1275 try {
1276 try (OutputStream fos = new FileOutputStream(groupsFile)) {
1277 DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos);
1278 for (GroupInfo record : account.getGroupStore().getGroups()) {
1279 ThreadInfo info = account.getThreadStore().getThread(Base64.encodeBytes(record.groupId));
1280 out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name),
1281 new ArrayList<>(record.members), createGroupAvatarAttachment(record.groupId),
1282 record.active, Optional.fromNullable(info != null ? info.messageExpirationTime : null),
1283 Optional.fromNullable(record.color), false));
1284 }
1285 }
1286
1287 if (groupsFile.exists() && groupsFile.length() > 0) {
1288 try (FileInputStream groupsFileStream = new FileInputStream(groupsFile)) {
1289 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
1290 .withStream(groupsFileStream)
1291 .withContentType("application/octet-stream")
1292 .withLength(groupsFile.length())
1293 .build();
1294
1295 sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
1296 }
1297 }
1298 } finally {
1299 try {
1300 Files.delete(groupsFile.toPath());
1301 } catch (IOException e) {
1302 System.err.println("Failed to delete groups temp file “" + groupsFile + "”: " + e.getMessage());
1303 }
1304 }
1305 }
1306
1307 private void sendContacts() throws IOException, UntrustedIdentityException {
1308 File contactsFile = IOUtils.createTempFile();
1309
1310 try {
1311 try (OutputStream fos = new FileOutputStream(contactsFile)) {
1312 DeviceContactsOutputStream out = new DeviceContactsOutputStream(fos);
1313 for (ContactInfo record : account.getContactStore().getContacts()) {
1314 VerifiedMessage verifiedMessage = null;
1315 ThreadInfo info = account.getThreadStore().getThread(record.number);
1316 if (getIdentities().containsKey(record.number)) {
1317 JsonIdentityKeyStore.Identity currentIdentity = null;
1318 for (JsonIdentityKeyStore.Identity id : getIdentities().get(record.number)) {
1319 if (currentIdentity == null || id.getDateAdded().after(currentIdentity.getDateAdded())) {
1320 currentIdentity = id;
1321 }
1322 }
1323 if (currentIdentity != null) {
1324 verifiedMessage = new VerifiedMessage(record.number, currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime());
1325 }
1326 }
1327
1328 byte[] profileKey = record.profileKey == null ? null : Base64.decode(record.profileKey);
1329 // TODO store list of blocked numbers
1330 boolean blocked = false;
1331 out.write(new DeviceContact(record.number, Optional.fromNullable(record.name),
1332 createContactAvatarAttachment(record.number), Optional.fromNullable(record.color),
1333 Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), blocked, Optional.fromNullable(info != null ? info.messageExpirationTime : null)));
1334 }
1335
1336 if (account.getProfileKey() != null) {
1337 // Send our own profile key as well
1338 out.write(new DeviceContact(account.getUsername(),
1339 Optional.<String>absent(), Optional.<SignalServiceAttachmentStream>absent(),
1340 Optional.<String>absent(), Optional.<VerifiedMessage>absent(),
1341 Optional.of(account.getProfileKey()),
1342 false, Optional.<Integer>absent()));
1343 }
1344 }
1345
1346 if (contactsFile.exists() && contactsFile.length() > 0) {
1347 try (FileInputStream contactsFileStream = new FileInputStream(contactsFile)) {
1348 SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
1349 .withStream(contactsFileStream)
1350 .withContentType("application/octet-stream")
1351 .withLength(contactsFile.length())
1352 .build();
1353
1354 sendSyncMessage(SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, true)));
1355 }
1356 }
1357 } finally {
1358 try {
1359 Files.delete(contactsFile.toPath());
1360 } catch (IOException e) {
1361 System.err.println("Failed to delete contacts temp file “" + contactsFile + "”: " + e.getMessage());
1362 }
1363 }
1364 }
1365
1366 private void sendVerifiedMessage(String destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
1367 VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis());
1368 sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
1369 }
1370
1371 public ContactInfo getContact(String number) {
1372 return account.getContactStore().getContact(number);
1373 }
1374
1375 public GroupInfo getGroup(byte[] groupId) {
1376 return account.getGroupStore().getGroup(groupId);
1377 }
1378
1379 public Map<String, List<JsonIdentityKeyStore.Identity>> getIdentities() {
1380 return account.getSignalProtocolStore().getIdentities();
1381 }
1382
1383 public List<JsonIdentityKeyStore.Identity> getIdentities(String number) {
1384 return account.getSignalProtocolStore().getIdentities(number);
1385 }
1386
1387 /**
1388 * Trust this the identity with this fingerprint
1389 *
1390 * @param name username of the identity
1391 * @param fingerprint Fingerprint
1392 */
1393 public boolean trustIdentityVerified(String name, byte[] fingerprint) {
1394 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(name);
1395 if (ids == null) {
1396 return false;
1397 }
1398 for (JsonIdentityKeyStore.Identity id : ids) {
1399 if (!Arrays.equals(id.getIdentityKey().serialize(), fingerprint)) {
1400 continue;
1401 }
1402
1403 account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1404 try {
1405 sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1406 } catch (IOException | UntrustedIdentityException e) {
1407 e.printStackTrace();
1408 }
1409 account.save();
1410 return true;
1411 }
1412 return false;
1413 }
1414
1415 /**
1416 * Trust this the identity with this safety number
1417 *
1418 * @param name username of the identity
1419 * @param safetyNumber Safety number
1420 */
1421 public boolean trustIdentityVerifiedSafetyNumber(String name, String safetyNumber) {
1422 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(name);
1423 if (ids == null) {
1424 return false;
1425 }
1426 for (JsonIdentityKeyStore.Identity id : ids) {
1427 if (!safetyNumber.equals(computeSafetyNumber(name, id.getIdentityKey()))) {
1428 continue;
1429 }
1430
1431 account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1432 try {
1433 sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
1434 } catch (IOException | UntrustedIdentityException e) {
1435 e.printStackTrace();
1436 }
1437 account.save();
1438 return true;
1439 }
1440 return false;
1441 }
1442
1443 /**
1444 * Trust all keys of this identity without verification
1445 *
1446 * @param name username of the identity
1447 */
1448 public boolean trustIdentityAllKeys(String name) {
1449 List<JsonIdentityKeyStore.Identity> ids = account.getSignalProtocolStore().getIdentities(name);
1450 if (ids == null) {
1451 return false;
1452 }
1453 for (JsonIdentityKeyStore.Identity id : ids) {
1454 if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
1455 account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
1456 try {
1457 sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
1458 } catch (IOException | UntrustedIdentityException e) {
1459 e.printStackTrace();
1460 }
1461 }
1462 }
1463 account.save();
1464 return true;
1465 }
1466
1467 public String computeSafetyNumber(String theirUsername, IdentityKey theirIdentityKey) {
1468 return Utils.computeSafetyNumber(username, getIdentity(), theirUsername, theirIdentityKey);
1469 }
1470
1471 public interface ReceiveMessageHandler {
1472
1473 void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, Throwable e);
1474 }
1475 }