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