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