2 * Copyright (C) 2015 AsamK
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.
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.
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/>.
17 package org
.asamk
.signal
;
19 import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
20 import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
21 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
22 import com
.fasterxml
.jackson
.core
.JsonParser
;
23 import com
.fasterxml
.jackson
.databind
.DeserializationFeature
;
24 import com
.fasterxml
.jackson
.databind
.JsonNode
;
25 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
26 import com
.fasterxml
.jackson
.databind
.SerializationFeature
;
27 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
28 import org
.apache
.http
.util
.TextUtils
;
29 import org
.asamk
.Signal
;
30 import org
.asamk
.signal
.storage
.contacts
.ContactInfo
;
31 import org
.asamk
.signal
.storage
.contacts
.JsonContactsStore
;
32 import org
.asamk
.signal
.storage
.groups
.GroupInfo
;
33 import org
.asamk
.signal
.storage
.groups
.JsonGroupStore
;
34 import org
.asamk
.signal
.storage
.protocol
.JsonIdentityKeyStore
;
35 import org
.asamk
.signal
.storage
.protocol
.JsonSignalProtocolStore
;
36 import org
.asamk
.signal
.storage
.threads
.JsonThreadStore
;
37 import org
.asamk
.signal
.storage
.threads
.ThreadInfo
;
38 import org
.asamk
.signal
.util
.Base64
;
39 import org
.asamk
.signal
.util
.Util
;
40 import org
.whispersystems
.libsignal
.*;
41 import org
.whispersystems
.libsignal
.ecc
.Curve
;
42 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
43 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
44 import org
.whispersystems
.libsignal
.fingerprint
.Fingerprint
;
45 import org
.whispersystems
.libsignal
.fingerprint
.NumericFingerprintGenerator
;
46 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
47 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
48 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
49 import org
.whispersystems
.libsignal
.util
.Medium
;
50 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
51 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
52 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
53 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
54 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
55 import org
.whispersystems
.signalservice
.api
.crypto
.SignalServiceCipher
;
56 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
57 import org
.whispersystems
.signalservice
.api
.messages
.*;
58 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.*;
59 import org
.whispersystems
.signalservice
.api
.push
.ContactTokenDetails
;
60 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
61 import org
.whispersystems
.signalservice
.api
.push
.TrustStore
;
62 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.*;
63 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
64 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
65 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceProtos
;
66 import org
.whispersystems
.signalservice
.internal
.push
.SignalServiceUrl
;
70 import java
.net
.URISyntaxException
;
71 import java
.net
.URLDecoder
;
72 import java
.net
.URLEncoder
;
73 import java
.nio
.channels
.Channels
;
74 import java
.nio
.channels
.FileChannel
;
75 import java
.nio
.channels
.FileLock
;
76 import java
.nio
.file
.Files
;
77 import java
.nio
.file
.Path
;
78 import java
.nio
.file
.Paths
;
79 import java
.nio
.file
.StandardCopyOption
;
80 import java
.nio
.file
.attribute
.PosixFilePermission
;
81 import java
.nio
.file
.attribute
.PosixFilePermissions
;
83 import java
.util
.concurrent
.TimeUnit
;
84 import java
.util
.concurrent
.TimeoutException
;
86 import static java
.nio
.file
.attribute
.PosixFilePermission
.*;
88 class Manager
implements Signal
{
89 private final static String URL
= "https://textsecure-service.whispersystems.org";
90 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
91 private final static SignalServiceUrl
[] serviceUrls
= new SignalServiceUrl
[]{new SignalServiceUrl(URL
, TRUST_STORE
)};
93 public final static String PROJECT_NAME
= Manager
.class.getPackage().getImplementationTitle();
94 public final static String PROJECT_VERSION
= Manager
.class.getPackage().getImplementationVersion();
95 private final static String USER_AGENT
= PROJECT_NAME
== null ?
null : PROJECT_NAME
+ " " + PROJECT_VERSION
;
97 private final static int PREKEY_MINIMUM_COUNT
= 20;
98 private static final int PREKEY_BATCH_SIZE
= 100;
99 private static final int MAX_ATTACHMENT_SIZE
= 150 * 1024 * 1024;
101 private final String settingsPath
;
102 private final String dataPath
;
103 private final String attachmentsPath
;
104 private final String avatarsPath
;
106 private FileChannel fileChannel
;
107 private FileLock lock
;
109 private final ObjectMapper jsonProcessor
= new ObjectMapper();
110 private String username
;
111 private int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
112 private String password
;
113 private String signalingKey
;
114 private int preKeyIdOffset
;
115 private int nextSignedPreKeyId
;
117 private boolean registered
= false;
119 private JsonSignalProtocolStore signalProtocolStore
;
120 private SignalServiceAccountManager accountManager
;
121 private JsonGroupStore groupStore
;
122 private JsonContactsStore contactStore
;
123 private JsonThreadStore threadStore
;
124 private SignalServiceMessagePipe messagePipe
= null;
126 public Manager(String username
, String settingsPath
) {
127 this.username
= username
;
128 this.settingsPath
= settingsPath
;
129 this.dataPath
= this.settingsPath
+ "/data";
130 this.attachmentsPath
= this.settingsPath
+ "/attachments";
131 this.avatarsPath
= this.settingsPath
+ "/avatars";
133 jsonProcessor
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
134 jsonProcessor
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
135 jsonProcessor
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
136 jsonProcessor
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
137 jsonProcessor
.disable(JsonParser
.Feature
.AUTO_CLOSE_SOURCE
);
138 jsonProcessor
.disable(JsonGenerator
.Feature
.AUTO_CLOSE_TARGET
);
141 public String
getUsername() {
145 private IdentityKey
getIdentity() {
146 return signalProtocolStore
.getIdentityKeyPair().getPublicKey();
149 public int getDeviceId() {
153 public String
getFileName() {
154 return dataPath
+ "/" + username
;
157 private String
getMessageCachePath() {
158 return this.dataPath
+ "/" + username
+ ".d/msg-cache";
161 private String
getMessageCachePath(String sender
) {
162 return getMessageCachePath() + "/" + sender
.replace("/", "_");
165 private File
getMessageCacheFile(String sender
, long now
, long timestamp
) throws IOException
{
166 String cachePath
= getMessageCachePath(sender
);
167 createPrivateDirectories(cachePath
);
168 return new File(cachePath
+ "/" + now
+ "_" + timestamp
);
171 private static void createPrivateDirectories(String path
) throws IOException
{
172 final Path file
= new File(path
).toPath();
174 Set
<PosixFilePermission
> perms
= EnumSet
.of(OWNER_READ
, OWNER_WRITE
, OWNER_EXECUTE
);
175 Files
.createDirectories(file
, PosixFilePermissions
.asFileAttribute(perms
));
176 } catch (UnsupportedOperationException e
) {
177 Files
.createDirectories(file
);
181 private static void createPrivateFile(String path
) throws IOException
{
182 final Path file
= new File(path
).toPath();
184 Set
<PosixFilePermission
> perms
= EnumSet
.of(OWNER_READ
, OWNER_WRITE
);
185 Files
.createFile(file
, PosixFilePermissions
.asFileAttribute(perms
));
186 } catch (UnsupportedOperationException e
) {
187 Files
.createFile(file
);
191 public boolean userExists() {
192 if (username
== null) {
195 File f
= new File(getFileName());
196 return !(!f
.exists() || f
.isDirectory());
199 public boolean userHasKeys() {
200 return signalProtocolStore
!= null;
203 private JsonNode
getNotNullNode(JsonNode parent
, String name
) throws InvalidObjectException
{
204 JsonNode node
= parent
.get(name
);
206 throw new InvalidObjectException(String
.format("Incorrect file format: expected parameter %s not found ", name
));
212 private void openFileChannel() throws IOException
{
213 if (fileChannel
!= null)
216 createPrivateDirectories(dataPath
);
217 if (!new File(getFileName()).exists()) {
218 createPrivateFile(getFileName());
220 fileChannel
= new RandomAccessFile(new File(getFileName()), "rw").getChannel();
221 lock
= fileChannel
.tryLock();
223 System
.err
.println("Config file is in use by another instance, waiting…");
224 lock
= fileChannel
.lock();
225 System
.err
.println("Config file lock acquired.");
229 public void init() throws IOException
{
232 migrateLegacyConfigs();
234 accountManager
= new SignalServiceAccountManager(serviceUrls
, username
, password
, deviceId
, USER_AGENT
);
236 if (registered
&& accountManager
.getPreKeysCount() < PREKEY_MINIMUM_COUNT
) {
240 } catch (AuthorizationFailedException e
) {
241 System
.err
.println("Authorization failed, was the number registered elsewhere?");
245 private void load() throws IOException
{
247 JsonNode rootNode
= jsonProcessor
.readTree(Channels
.newInputStream(fileChannel
));
249 JsonNode node
= rootNode
.get("deviceId");
251 deviceId
= node
.asInt();
253 username
= getNotNullNode(rootNode
, "username").asText();
254 password
= getNotNullNode(rootNode
, "password").asText();
255 if (rootNode
.has("signalingKey")) {
256 signalingKey
= getNotNullNode(rootNode
, "signalingKey").asText();
258 if (rootNode
.has("preKeyIdOffset")) {
259 preKeyIdOffset
= getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
263 if (rootNode
.has("nextSignedPreKeyId")) {
264 nextSignedPreKeyId
= getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
266 nextSignedPreKeyId
= 0;
268 signalProtocolStore
= jsonProcessor
.convertValue(getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
269 registered
= getNotNullNode(rootNode
, "registered").asBoolean();
270 JsonNode groupStoreNode
= rootNode
.get("groupStore");
271 if (groupStoreNode
!= null) {
272 groupStore
= jsonProcessor
.convertValue(groupStoreNode
, JsonGroupStore
.class);
274 if (groupStore
== null) {
275 groupStore
= new JsonGroupStore();
278 JsonNode contactStoreNode
= rootNode
.get("contactStore");
279 if (contactStoreNode
!= null) {
280 contactStore
= jsonProcessor
.convertValue(contactStoreNode
, JsonContactsStore
.class);
282 if (contactStore
== null) {
283 contactStore
= new JsonContactsStore();
285 JsonNode threadStoreNode
= rootNode
.get("threadStore");
286 if (threadStoreNode
!= null) {
287 threadStore
= jsonProcessor
.convertValue(threadStoreNode
, JsonThreadStore
.class);
289 if (threadStore
== null) {
290 threadStore
= new JsonThreadStore();
294 private void migrateLegacyConfigs() {
295 // Copy group avatars that were previously stored in the attachments folder
296 // to the new avatar folder
297 if (JsonGroupStore
.groupsWithLegacyAvatarId
.size() > 0) {
298 for (GroupInfo g
: JsonGroupStore
.groupsWithLegacyAvatarId
) {
299 File avatarFile
= getGroupAvatarFile(g
.groupId
);
300 File attachmentFile
= getAttachmentFile(g
.getAvatarId());
301 if (!avatarFile
.exists() && attachmentFile
.exists()) {
303 createPrivateDirectories(avatarsPath
);
304 Files
.copy(attachmentFile
.toPath(), avatarFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
305 } catch (Exception e
) {
310 JsonGroupStore
.groupsWithLegacyAvatarId
.clear();
315 private void save() {
316 if (username
== null) {
319 ObjectNode rootNode
= jsonProcessor
.createObjectNode();
320 rootNode
.put("username", username
)
321 .put("deviceId", deviceId
)
322 .put("password", password
)
323 .put("signalingKey", signalingKey
)
324 .put("preKeyIdOffset", preKeyIdOffset
)
325 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
326 .put("registered", registered
)
327 .putPOJO("axolotlStore", signalProtocolStore
)
328 .putPOJO("groupStore", groupStore
)
329 .putPOJO("contactStore", contactStore
)
330 .putPOJO("threadStore", threadStore
)
334 fileChannel
.position(0);
335 jsonProcessor
.writeValue(Channels
.newOutputStream(fileChannel
), rootNode
);
336 fileChannel
.truncate(fileChannel
.position());
337 fileChannel
.force(false);
338 } catch (Exception e
) {
339 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
343 public void createNewIdentity() {
344 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
345 int registrationId
= KeyHelper
.generateRegistrationId(false);
346 signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
347 groupStore
= new JsonGroupStore();
352 public boolean isRegistered() {
356 public void register(boolean voiceVerification
) throws IOException
{
357 password
= Util
.getSecret(18);
359 accountManager
= new SignalServiceAccountManager(serviceUrls
, username
, password
, USER_AGENT
);
361 if (voiceVerification
)
362 accountManager
.requestVoiceVerificationCode();
364 accountManager
.requestSmsVerificationCode();
370 public void updateAccountAttributes() throws IOException
{
371 accountManager
.setAccountAttributes(signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, false, true);
374 public void unregister() throws IOException
{
375 // When setting an empty GCM id, the Signal-Server also sets the fetchesMessages property to false.
376 // If this is the master device, other users can't send messages to this number anymore.
377 // If this is a linked device, other users can still send messages, but this device doesn't receive them anymore.
378 accountManager
.setGcmId(Optional
.<String
>absent());
381 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
382 password
= Util
.getSecret(18);
384 accountManager
= new SignalServiceAccountManager(serviceUrls
, username
, password
, USER_AGENT
);
385 String uuid
= accountManager
.getNewDeviceUuid();
389 return new URI("tsdevice:/?uuid=" + URLEncoder
.encode(uuid
, "utf-8") + "&pub_key=" + URLEncoder
.encode(Base64
.encodeBytesWithoutPadding(signalProtocolStore
.getIdentityKeyPair().getPublicKey().serialize()), "utf-8"));
390 } catch (URISyntaxException e
) {
396 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
397 signalingKey
= Util
.getSecret(52);
398 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(signalProtocolStore
.getIdentityKeyPair(), signalingKey
, false, true, signalProtocolStore
.getLocalRegistrationId(), deviceName
);
399 deviceId
= ret
.getDeviceId();
400 username
= ret
.getNumber();
401 // TODO do this check before actually registering
403 throw new UserAlreadyExists(username
, getFileName());
405 signalProtocolStore
= new JsonSignalProtocolStore(ret
.getIdentity(), signalProtocolStore
.getLocalRegistrationId());
411 requestSyncContacts();
416 public List
<DeviceInfo
> getLinkedDevices() throws IOException
{
417 return accountManager
.getDevices();
420 public void removeLinkedDevices(int deviceId
) throws IOException
{
421 accountManager
.removeDevice(deviceId
);
424 public static Map
<String
, String
> getQueryMap(String query
) {
425 String
[] params
= query
.split("&");
426 Map
<String
, String
> map
= new HashMap
<>();
427 for (String param
: params
) {
430 name
= URLDecoder
.decode(param
.split("=")[0], "utf-8");
431 } catch (UnsupportedEncodingException e
) {
436 value
= URLDecoder
.decode(param
.split("=")[1], "utf-8");
437 } catch (UnsupportedEncodingException e
) {
440 map
.put(name
, value
);
445 public void addDeviceLink(URI linkUri
) throws IOException
, InvalidKeyException
{
446 Map
<String
, String
> query
= getQueryMap(linkUri
.getRawQuery());
447 String deviceIdentifier
= query
.get("uuid");
448 String publicKeyEncoded
= query
.get("pub_key");
450 if (TextUtils
.isEmpty(deviceIdentifier
) || TextUtils
.isEmpty(publicKeyEncoded
)) {
451 throw new RuntimeException("Invalid device link uri");
454 ECPublicKey deviceKey
= Curve
.decodePoint(Base64
.decode(publicKeyEncoded
), 0);
456 addDevice(deviceIdentifier
, deviceKey
);
459 private void addDevice(String deviceIdentifier
, ECPublicKey deviceKey
) throws IOException
, InvalidKeyException
{
460 IdentityKeyPair identityKeyPair
= signalProtocolStore
.getIdentityKeyPair();
461 String verificationCode
= accountManager
.getNewDeviceVerificationCode();
463 accountManager
.addDevice(deviceIdentifier
, deviceKey
, identityKeyPair
, verificationCode
);
466 private List
<PreKeyRecord
> generatePreKeys() {
467 List
<PreKeyRecord
> records
= new LinkedList
<>();
469 for (int i
= 0; i
< PREKEY_BATCH_SIZE
; i
++) {
470 int preKeyId
= (preKeyIdOffset
+ i
) % Medium
.MAX_VALUE
;
471 ECKeyPair keyPair
= Curve
.generateKeyPair();
472 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
474 signalProtocolStore
.storePreKey(preKeyId
, record);
478 preKeyIdOffset
= (preKeyIdOffset
+ PREKEY_BATCH_SIZE
+ 1) % Medium
.MAX_VALUE
;
484 private PreKeyRecord
getOrGenerateLastResortPreKey() {
485 if (signalProtocolStore
.containsPreKey(Medium
.MAX_VALUE
)) {
487 return signalProtocolStore
.loadPreKey(Medium
.MAX_VALUE
);
488 } catch (InvalidKeyIdException e
) {
489 signalProtocolStore
.removePreKey(Medium
.MAX_VALUE
);
493 ECKeyPair keyPair
= Curve
.generateKeyPair();
494 PreKeyRecord
record = new PreKeyRecord(Medium
.MAX_VALUE
, keyPair
);
496 signalProtocolStore
.storePreKey(Medium
.MAX_VALUE
, record);
502 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
504 ECKeyPair keyPair
= Curve
.generateKeyPair();
505 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
506 SignedPreKeyRecord
record = new SignedPreKeyRecord(nextSignedPreKeyId
, System
.currentTimeMillis(), keyPair
, signature
);
508 signalProtocolStore
.storeSignedPreKey(nextSignedPreKeyId
, record);
509 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
513 } catch (InvalidKeyException e
) {
514 throw new AssertionError(e
);
518 public void verifyAccount(String verificationCode
) throws IOException
{
519 verificationCode
= verificationCode
.replace("-", "");
520 signalingKey
= Util
.getSecret(52);
521 accountManager
.verifyAccountWithCode(verificationCode
, signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, false, true);
523 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
530 private void refreshPreKeys() throws IOException
{
531 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
532 PreKeyRecord lastResortKey
= getOrGenerateLastResortPreKey();
533 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(signalProtocolStore
.getIdentityKeyPair());
535 accountManager
.setPreKeys(signalProtocolStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
539 private static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
540 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
541 if (attachments
!= null) {
542 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
543 for (String attachment
: attachments
) {
545 SignalServiceAttachments
.add(createAttachment(new File(attachment
)));
546 } catch (IOException e
) {
547 throw new AttachmentInvalidException(attachment
, e
);
551 return SignalServiceAttachments
;
554 private static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
555 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
556 final long attachmentSize
= attachmentFile
.length();
557 String mime
= Files
.probeContentType(attachmentFile
.toPath());
559 mime
= "application/octet-stream";
561 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, Optional
.of(attachmentFile
.getName()), null);
564 private Optional
<SignalServiceAttachmentStream
> createGroupAvatarAttachment(byte[] groupId
) throws IOException
{
565 File file
= getGroupAvatarFile(groupId
);
566 if (!file
.exists()) {
567 return Optional
.absent();
570 return Optional
.of(createAttachment(file
));
573 private Optional
<SignalServiceAttachmentStream
> createContactAvatarAttachment(String number
) throws IOException
{
574 File file
= getContactAvatarFile(number
);
575 if (!file
.exists()) {
576 return Optional
.absent();
579 return Optional
.of(createAttachment(file
));
582 private GroupInfo
getGroupForSending(byte[] groupId
) throws GroupNotFoundException
, NotAGroupMemberException
{
583 GroupInfo g
= groupStore
.getGroup(groupId
);
585 throw new GroupNotFoundException(groupId
);
587 for (String member
: g
.members
) {
588 if (member
.equals(this.username
)) {
592 throw new NotAGroupMemberException(groupId
, g
.name
);
595 public List
<GroupInfo
> getGroups() {
596 return groupStore
.getGroups();
600 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
602 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
603 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
604 if (attachments
!= null) {
605 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
607 if (groupId
!= null) {
608 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
611 messageBuilder
.asGroupMessage(group
);
613 ThreadInfo thread
= threadStore
.getThread(Base64
.encodeBytes(groupId
));
614 if (thread
!= null) {
615 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
618 final GroupInfo g
= getGroupForSending(groupId
);
620 // Don't send group message to ourself
621 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
622 membersSend
.remove(this.username
);
623 sendMessage(messageBuilder
, membersSend
);
626 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
{
627 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
631 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
632 .asGroupMessage(group
);
634 final GroupInfo g
= getGroupForSending(groupId
);
635 g
.members
.remove(this.username
);
636 groupStore
.updateGroup(g
);
638 sendMessage(messageBuilder
, g
.members
);
641 private static String
join(CharSequence separator
, Iterable
<?
extends CharSequence
> list
) {
642 StringBuilder buf
= new StringBuilder();
643 for (CharSequence str
: list
) {
644 if (buf
.length() > 0) {
645 buf
.append(separator
);
650 return buf
.toString();
653 public byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
655 if (groupId
== null) {
657 g
= new GroupInfo(Util
.getSecretBytes(16));
658 g
.members
.add(username
);
660 g
= getGroupForSending(groupId
);
667 if (members
!= null) {
668 Set
<String
> newMembers
= new HashSet
<>();
669 for (String member
: members
) {
671 member
= canonicalizeNumber(member
);
672 } catch (InvalidNumberException e
) {
673 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
674 System
.err
.println("Aborting…");
677 if (g
.members
.contains(member
)) {
680 newMembers
.add(member
);
681 g
.members
.add(member
);
683 final List
<ContactTokenDetails
> contacts
= accountManager
.getContacts(newMembers
);
684 if (contacts
.size() != newMembers
.size()) {
685 // Some of the new members are not registered on Signal
686 for (ContactTokenDetails contact
: contacts
) {
687 newMembers
.remove(contact
.getNumber());
689 System
.err
.println("Failed to add members " + join(", ", newMembers
) + " to group: Not registered on Signal");
690 System
.err
.println("Aborting…");
695 if (avatarFile
!= null) {
696 createPrivateDirectories(avatarsPath
);
697 File aFile
= getGroupAvatarFile(g
.groupId
);
698 Files
.copy(Paths
.get(avatarFile
), aFile
.toPath(), StandardCopyOption
.REPLACE_EXISTING
);
701 groupStore
.updateGroup(g
);
703 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
705 // Don't send group message to ourself
706 final List
<String
> membersSend
= new ArrayList
<>(g
.members
);
707 membersSend
.remove(this.username
);
708 sendMessage(messageBuilder
, membersSend
);
712 private void sendUpdateGroupMessage(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
713 if (groupId
== null) {
716 GroupInfo g
= getGroupForSending(groupId
);
718 if (!g
.members
.contains(recipient
)) {
722 SignalServiceDataMessage
.Builder messageBuilder
= getGroupUpdateMessageBuilder(g
);
724 // Send group message only to the recipient who requested it
725 final List
<String
> membersSend
= new ArrayList
<>();
726 membersSend
.add(recipient
);
727 sendMessage(messageBuilder
, membersSend
);
730 private SignalServiceDataMessage
.Builder
getGroupUpdateMessageBuilder(GroupInfo g
) {
731 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
734 .withMembers(new ArrayList
<>(g
.members
));
736 File aFile
= getGroupAvatarFile(g
.groupId
);
737 if (aFile
.exists()) {
739 group
.withAvatar(createAttachment(aFile
));
740 } catch (IOException e
) {
741 throw new AttachmentInvalidException(aFile
.toString(), e
);
745 return SignalServiceDataMessage
.newBuilder()
746 .asGroupMessage(group
.build());
749 private void sendGroupInfoRequest(byte[] groupId
, String recipient
) throws IOException
, EncapsulatedExceptions
{
750 if (groupId
== null) {
754 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.REQUEST_INFO
)
757 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
758 .asGroupMessage(group
.build());
760 // Send group info request message to the recipient who sent us a message with this groupId
761 final List
<String
> membersSend
= new ArrayList
<>();
762 membersSend
.add(recipient
);
763 sendMessage(messageBuilder
, membersSend
);
767 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
768 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
{
769 List
<String
> recipients
= new ArrayList
<>(1);
770 recipients
.add(recipient
);
771 sendMessage(message
, attachments
, recipients
);
775 public void sendMessage(String messageText
, List
<String
> attachments
,
776 List
<String
> recipients
)
777 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
{
778 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
779 if (attachments
!= null) {
780 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
782 sendMessage(messageBuilder
, recipients
);
786 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
{
787 SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder()
788 .asEndSessionMessage();
790 sendMessage(messageBuilder
, recipients
);
794 public String
getContactName(String number
) {
795 ContactInfo contact
= contactStore
.getContact(number
);
796 if (contact
== null) {
804 public void setContactName(String number
, String name
) {
805 ContactInfo contact
= contactStore
.getContact(number
);
806 if (contact
== null) {
807 contact
= new ContactInfo();
808 contact
.number
= number
;
809 System
.out
.println("Add contact " + number
+ " named " + name
);
811 System
.out
.println("Updating contact " + number
+ " name " + contact
.name
+ " -> " + name
);
814 contactStore
.updateContact(contact
);
819 public String
getGroupName(byte[] groupId
) {
820 GroupInfo group
= getGroup(groupId
);
829 public List
<String
> getGroupMembers(byte[] groupId
) {
830 GroupInfo group
= getGroup(groupId
);
832 return new ArrayList
<String
>();
834 return new ArrayList
<String
>(group
.members
);
839 public byte[] updateGroup(byte[] groupId
, String name
, List
<String
> members
, String avatar
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
{
840 if (groupId
.length
== 0) {
843 if (name
.isEmpty()) {
846 if (members
.size() == 0) {
849 if (avatar
.isEmpty()) {
852 return sendUpdateGroupMessage(groupId
, name
, members
, avatar
);
855 private void requestSyncGroups() throws IOException
{
856 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.GROUPS
).build();
857 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
859 sendSyncMessage(message
);
860 } catch (UntrustedIdentityException e
) {
865 private void requestSyncContacts() throws IOException
{
866 SignalServiceProtos
.SyncMessage
.Request r
= SignalServiceProtos
.SyncMessage
.Request
.newBuilder().setType(SignalServiceProtos
.SyncMessage
.Request
.Type
.CONTACTS
).build();
867 SignalServiceSyncMessage message
= SignalServiceSyncMessage
.forRequest(new RequestMessage(r
));
869 sendSyncMessage(message
);
870 } catch (UntrustedIdentityException e
) {
875 private void sendSyncMessage(SignalServiceSyncMessage message
)
876 throws IOException
, UntrustedIdentityException
{
877 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(serviceUrls
, username
, password
,
878 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.fromNullable(messagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
880 messageSender
.sendMessage(message
);
881 } catch (UntrustedIdentityException e
) {
882 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
887 private void sendMessage(SignalServiceDataMessage
.Builder messageBuilder
, Collection
<String
> recipients
)
888 throws EncapsulatedExceptions
, IOException
{
889 Set
<SignalServiceAddress
> recipientsTS
= getSignalServiceAddresses(recipients
);
890 if (recipientsTS
== null) return;
892 SignalServiceDataMessage message
= null;
894 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(serviceUrls
, username
, password
,
895 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.fromNullable(messagePipe
), Optional
.<SignalServiceMessageSender
.EventListener
>absent());
897 message
= messageBuilder
.build();
898 if (message
.getGroupInfo().isPresent()) {
900 messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), message
);
901 } catch (EncapsulatedExceptions encapsulatedExceptions
) {
902 for (UntrustedIdentityException e
: encapsulatedExceptions
.getUntrustedIdentityExceptions()) {
903 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
907 // Send to all individually, so sync messages are sent correctly
908 List
<UntrustedIdentityException
> untrustedIdentities
= new LinkedList
<>();
909 List
<UnregisteredUserException
> unregisteredUsers
= new LinkedList
<>();
910 List
<NetworkFailureException
> networkExceptions
= new LinkedList
<>();
911 for (SignalServiceAddress address
: recipientsTS
) {
912 ThreadInfo thread
= threadStore
.getThread(address
.getNumber());
913 if (thread
!= null) {
914 messageBuilder
.withExpiration(thread
.messageExpirationTime
);
916 messageBuilder
.withExpiration(0);
918 message
= messageBuilder
.build();
920 messageSender
.sendMessage(address
, message
);
921 } catch (UntrustedIdentityException e
) {
922 signalProtocolStore
.saveIdentity(e
.getE164Number(), e
.getIdentityKey(), TrustLevel
.UNTRUSTED
);
923 untrustedIdentities
.add(e
);
924 } catch (UnregisteredUserException e
) {
925 unregisteredUsers
.add(e
);
926 } catch (PushNetworkException e
) {
927 networkExceptions
.add(new NetworkFailureException(address
.getNumber(), e
));
930 if (!untrustedIdentities
.isEmpty() || !unregisteredUsers
.isEmpty() || !networkExceptions
.isEmpty()) {
931 throw new EncapsulatedExceptions(untrustedIdentities
, unregisteredUsers
, networkExceptions
);
935 if (message
!= null && message
.isEndSession()) {
936 for (SignalServiceAddress recipient
: recipientsTS
) {
937 handleEndSession(recipient
.getNumber());
944 private Set
<SignalServiceAddress
> getSignalServiceAddresses(Collection
<String
> recipients
) {
945 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
946 for (String recipient
: recipients
) {
948 recipientsTS
.add(getPushAddress(recipient
));
949 } catch (InvalidNumberException e
) {
950 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
951 System
.err
.println("Aborting sending.");
959 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) throws NoSessionException
, LegacyMessageException
, InvalidVersionException
, InvalidMessageException
, DuplicateMessageException
, InvalidKeyException
, InvalidKeyIdException
, org
.whispersystems
.libsignal
.UntrustedIdentityException
{
960 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), signalProtocolStore
);
962 return cipher
.decrypt(envelope
);
963 } catch (org
.whispersystems
.libsignal
.UntrustedIdentityException e
) {
964 signalProtocolStore
.saveIdentity(e
.getName(), e
.getUntrustedIdentity(), TrustLevel
.UNTRUSTED
);
969 private void handleEndSession(String source
) {
970 signalProtocolStore
.deleteAllSessions(source
);
973 public interface ReceiveMessageHandler
{
974 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, Throwable e
);
977 private void handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
, boolean ignoreAttachments
) {
979 if (message
.getGroupInfo().isPresent()) {
980 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
981 threadId
= Base64
.encodeBytes(groupInfo
.getGroupId());
982 GroupInfo group
= groupStore
.getGroup(groupInfo
.getGroupId());
983 switch (groupInfo
.getType()) {
986 group
= new GroupInfo(groupInfo
.getGroupId());
989 if (groupInfo
.getAvatar().isPresent()) {
990 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
991 if (avatar
.isPointer()) {
993 retrieveGroupAvatarAttachment(avatar
.asPointer(), group
.groupId
);
994 } catch (IOException
| InvalidMessageException e
) {
995 System
.err
.println("Failed to retrieve group avatar (" + avatar
.asPointer().getId() + "): " + e
.getMessage());
1000 if (groupInfo
.getName().isPresent()) {
1001 group
.name
= groupInfo
.getName().get();
1004 if (groupInfo
.getMembers().isPresent()) {
1005 group
.members
.addAll(groupInfo
.getMembers().get());
1008 groupStore
.updateGroup(group
);
1011 if (group
== null) {
1013 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
1014 } catch (IOException
| EncapsulatedExceptions e
) {
1015 e
.printStackTrace();
1020 if (group
== null) {
1022 sendGroupInfoRequest(groupInfo
.getGroupId(), source
);
1023 } catch (IOException
| EncapsulatedExceptions e
) {
1024 e
.printStackTrace();
1027 group
.members
.remove(source
);
1028 groupStore
.updateGroup(group
);
1032 if (group
!= null) {
1034 sendUpdateGroupMessage(groupInfo
.getGroupId(), source
);
1035 } catch (IOException
| EncapsulatedExceptions e
) {
1036 e
.printStackTrace();
1037 } catch (NotAGroupMemberException e
) {
1038 // We have left this group, so don't send a group update message
1045 threadId
= destination
;
1050 if (message
.isEndSession()) {
1051 handleEndSession(isSync ? destination
: source
);
1053 if (message
.isExpirationUpdate() || message
.getBody().isPresent()) {
1054 ThreadInfo thread
= threadStore
.getThread(threadId
);
1055 if (thread
== null) {
1056 thread
= new ThreadInfo();
1057 thread
.id
= threadId
;
1059 if (thread
.messageExpirationTime
!= message
.getExpiresInSeconds()) {
1060 thread
.messageExpirationTime
= message
.getExpiresInSeconds();
1061 threadStore
.updateThread(thread
);
1064 if (message
.getAttachments().isPresent() && !ignoreAttachments
) {
1065 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
1066 if (attachment
.isPointer()) {
1068 retrieveAttachment(attachment
.asPointer());
1069 } catch (IOException
| InvalidMessageException e
) {
1070 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
1077 public void retryFailedReceivedMessages(ReceiveMessageHandler handler
, boolean ignoreAttachments
) {
1078 final File cachePath
= new File(getMessageCachePath());
1079 if (!cachePath
.exists()) {
1082 for (final File dir
: cachePath
.listFiles()) {
1083 if (!dir
.isDirectory()) {
1087 for (final File fileEntry
: dir
.listFiles()) {
1088 if (!fileEntry
.isFile()) {
1091 SignalServiceEnvelope envelope
;
1093 envelope
= loadEnvelope(fileEntry
);
1094 if (envelope
== null) {
1097 } catch (IOException e
) {
1098 e
.printStackTrace();
1101 SignalServiceContent content
= null;
1102 if (!envelope
.isReceipt()) {
1104 content
= decryptMessage(envelope
);
1105 } catch (Exception e
) {
1108 handleMessage(envelope
, content
, ignoreAttachments
);
1111 handler
.handleMessage(envelope
, content
, null);
1113 Files
.delete(fileEntry
.toPath());
1114 } catch (IOException e
) {
1115 System
.out
.println("Failed to delete cached message file “" + fileEntry
+ "”: " + e
.getMessage());
1121 public void receiveMessages(long timeout
, TimeUnit unit
, boolean returnOnTimeout
, boolean ignoreAttachments
, ReceiveMessageHandler handler
) throws IOException
{
1122 retryFailedReceivedMessages(handler
, ignoreAttachments
);
1123 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(serviceUrls
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1126 if (messagePipe
== null) {
1127 messagePipe
= messageReceiver
.createMessagePipe();
1131 SignalServiceEnvelope envelope
;
1132 SignalServiceContent content
= null;
1133 Exception exception
= null;
1134 final long now
= new Date().getTime();
1136 envelope
= messagePipe
.read(timeout
, unit
, new SignalServiceMessagePipe
.MessagePipeCallback() {
1138 public void onMessage(SignalServiceEnvelope envelope
) {
1139 // store message on disk, before acknowledging receipt to the server
1141 File cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1142 storeEnvelope(envelope
, cacheFile
);
1143 } catch (IOException e
) {
1144 System
.err
.println("Failed to store encrypted message in disk cache, ignoring: " + e
.getMessage());
1148 } catch (TimeoutException e
) {
1149 if (returnOnTimeout
)
1152 } catch (InvalidVersionException e
) {
1153 System
.err
.println("Ignoring error: " + e
.getMessage());
1156 if (!envelope
.isReceipt()) {
1158 content
= decryptMessage(envelope
);
1159 } catch (Exception e
) {
1162 handleMessage(envelope
, content
, ignoreAttachments
);
1165 handler
.handleMessage(envelope
, content
, exception
);
1166 if (exception
== null || !(exception
instanceof org
.whispersystems
.libsignal
.UntrustedIdentityException
)) {
1167 File cacheFile
= null;
1169 cacheFile
= getMessageCacheFile(envelope
.getSource(), now
, envelope
.getTimestamp());
1170 Files
.delete(cacheFile
.toPath());
1171 } catch (IOException e
) {
1172 System
.out
.println("Failed to delete cached message file “" + cacheFile
+ "”: " + e
.getMessage());
1177 if (messagePipe
!= null) {
1178 messagePipe
.shutdown();
1184 private void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent content
, boolean ignoreAttachments
) {
1185 if (content
!= null) {
1186 if (content
.getDataMessage().isPresent()) {
1187 SignalServiceDataMessage message
= content
.getDataMessage().get();
1188 handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
, ignoreAttachments
);
1190 if (content
.getSyncMessage().isPresent()) {
1191 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
1192 if (syncMessage
.getSent().isPresent()) {
1193 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
1194 handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get(), ignoreAttachments
);
1196 if (syncMessage
.getRequest().isPresent()) {
1197 RequestMessage rm
= syncMessage
.getRequest().get();
1198 if (rm
.isContactsRequest()) {
1201 } catch (UntrustedIdentityException
| IOException e
) {
1202 e
.printStackTrace();
1205 if (rm
.isGroupsRequest()) {
1208 } catch (UntrustedIdentityException
| IOException e
) {
1209 e
.printStackTrace();
1213 if (syncMessage
.getGroups().isPresent()) {
1214 File tmpFile
= null;
1216 tmpFile
= Util
.createTempFile();
1217 DeviceGroupsInputStream s
= new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage
.getGroups().get().asPointer(), tmpFile
));
1219 while ((g
= s
.read()) != null) {
1220 GroupInfo syncGroup
= groupStore
.getGroup(g
.getId());
1221 if (syncGroup
== null) {
1222 syncGroup
= new GroupInfo(g
.getId());
1224 if (g
.getName().isPresent()) {
1225 syncGroup
.name
= g
.getName().get();
1227 syncGroup
.members
.addAll(g
.getMembers());
1228 syncGroup
.active
= g
.isActive();
1230 if (g
.getAvatar().isPresent()) {
1231 retrieveGroupAvatarAttachment(g
.getAvatar().get(), syncGroup
.groupId
);
1233 groupStore
.updateGroup(syncGroup
);
1235 } catch (Exception e
) {
1236 e
.printStackTrace();
1238 if (tmpFile
!= null) {
1240 Files
.delete(tmpFile
.toPath());
1241 } catch (IOException e
) {
1242 System
.out
.println("Failed to delete temp file “" + tmpFile
+ "”: " + e
.getMessage());
1246 if (syncMessage
.getBlockedList().isPresent()) {
1247 // TODO store list of blocked numbers
1250 if (syncMessage
.getContacts().isPresent()) {
1251 File tmpFile
= null;
1253 tmpFile
= Util
.createTempFile();
1254 DeviceContactsInputStream s
= new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage
.getContacts().get().asPointer(), tmpFile
));
1256 while ((c
= s
.read()) != null) {
1257 ContactInfo contact
= contactStore
.getContact(c
.getNumber());
1258 if (contact
== null) {
1259 contact
= new ContactInfo();
1260 contact
.number
= c
.getNumber();
1262 if (c
.getName().isPresent()) {
1263 contact
.name
= c
.getName().get();
1265 if (c
.getColor().isPresent()) {
1266 contact
.color
= c
.getColor().get();
1268 contactStore
.updateContact(contact
);
1270 if (c
.getAvatar().isPresent()) {
1271 retrieveContactAvatarAttachment(c
.getAvatar().get(), contact
.number
);
1274 } catch (Exception e
) {
1275 e
.printStackTrace();
1277 if (tmpFile
!= null) {
1279 Files
.delete(tmpFile
.toPath());
1280 } catch (IOException e
) {
1281 System
.out
.println("Failed to delete temp file “" + tmpFile
+ "”: " + e
.getMessage());
1290 private SignalServiceEnvelope
loadEnvelope(File file
) throws IOException
{
1291 try (FileInputStream f
= new FileInputStream(file
)) {
1292 DataInputStream
in = new DataInputStream(f
);
1293 int version
= in.readInt();
1297 int type
= in.readInt();
1298 String source
= in.readUTF();
1299 int sourceDevice
= in.readInt();
1300 String relay
= in.readUTF();
1301 long timestamp
= in.readLong();
1302 byte[] content
= null;
1303 int contentLen
= in.readInt();
1304 if (contentLen
> 0) {
1305 content
= new byte[contentLen
];
1306 in.readFully(content
);
1308 byte[] legacyMessage
= null;
1309 int legacyMessageLen
= in.readInt();
1310 if (legacyMessageLen
> 0) {
1311 legacyMessage
= new byte[legacyMessageLen
];
1312 in.readFully(legacyMessage
);
1314 return new SignalServiceEnvelope(type
, source
, sourceDevice
, relay
, timestamp
, legacyMessage
, content
);
1318 private void storeEnvelope(SignalServiceEnvelope envelope
, File file
) throws IOException
{
1319 try (FileOutputStream f
= new FileOutputStream(file
)) {
1320 try (DataOutputStream out
= new DataOutputStream(f
)) {
1321 out
.writeInt(1); // version
1322 out
.writeInt(envelope
.getType());
1323 out
.writeUTF(envelope
.getSource());
1324 out
.writeInt(envelope
.getSourceDevice());
1325 out
.writeUTF(envelope
.getRelay());
1326 out
.writeLong(envelope
.getTimestamp());
1327 if (envelope
.hasContent()) {
1328 out
.writeInt(envelope
.getContent().length
);
1329 out
.write(envelope
.getContent());
1333 if (envelope
.hasLegacyMessage()) {
1334 out
.writeInt(envelope
.getLegacyMessage().length
);
1335 out
.write(envelope
.getLegacyMessage());
1343 public File
getContactAvatarFile(String number
) {
1344 return new File(avatarsPath
, "contact-" + number
);
1347 private File
retrieveContactAvatarAttachment(SignalServiceAttachment attachment
, String number
) throws IOException
, InvalidMessageException
{
1348 createPrivateDirectories(avatarsPath
);
1349 if (attachment
.isPointer()) {
1350 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1351 return retrieveAttachment(pointer
, getContactAvatarFile(number
), false);
1353 SignalServiceAttachmentStream stream
= attachment
.asStream();
1354 return retrieveAttachment(stream
, getContactAvatarFile(number
));
1358 public File
getGroupAvatarFile(byte[] groupId
) {
1359 return new File(avatarsPath
, "group-" + Base64
.encodeBytes(groupId
).replace("/", "_"));
1362 private File
retrieveGroupAvatarAttachment(SignalServiceAttachment attachment
, byte[] groupId
) throws IOException
, InvalidMessageException
{
1363 createPrivateDirectories(avatarsPath
);
1364 if (attachment
.isPointer()) {
1365 SignalServiceAttachmentPointer pointer
= attachment
.asPointer();
1366 return retrieveAttachment(pointer
, getGroupAvatarFile(groupId
), false);
1368 SignalServiceAttachmentStream stream
= attachment
.asStream();
1369 return retrieveAttachment(stream
, getGroupAvatarFile(groupId
));
1373 public File
getAttachmentFile(long attachmentId
) {
1374 return new File(attachmentsPath
, attachmentId
+ "");
1377 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
1378 createPrivateDirectories(attachmentsPath
);
1379 return retrieveAttachment(pointer
, getAttachmentFile(pointer
.getId()), true);
1382 private File
retrieveAttachment(SignalServiceAttachmentStream stream
, File outputFile
) throws IOException
, InvalidMessageException
{
1383 InputStream input
= stream
.getInputStream();
1385 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1386 byte[] buffer
= new byte[4096];
1389 while ((read
= input
.read(buffer
)) != -1) {
1390 output
.write(buffer
, 0, read
);
1392 } catch (FileNotFoundException e
) {
1393 e
.printStackTrace();
1399 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
, File outputFile
, boolean storePreview
) throws IOException
, InvalidMessageException
{
1400 if (storePreview
&& pointer
.getPreview().isPresent()) {
1401 File previewFile
= new File(outputFile
+ ".preview");
1402 try (OutputStream output
= new FileOutputStream(previewFile
)) {
1403 byte[] preview
= pointer
.getPreview().get();
1404 output
.write(preview
, 0, preview
.length
);
1405 } catch (FileNotFoundException e
) {
1406 e
.printStackTrace();
1411 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(serviceUrls
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1413 File tmpFile
= Util
.createTempFile();
1414 try (InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
, MAX_ATTACHMENT_SIZE
)) {
1415 try (OutputStream output
= new FileOutputStream(outputFile
)) {
1416 byte[] buffer
= new byte[4096];
1419 while ((read
= input
.read(buffer
)) != -1) {
1420 output
.write(buffer
, 0, read
);
1422 } catch (FileNotFoundException e
) {
1423 e
.printStackTrace();
1428 Files
.delete(tmpFile
.toPath());
1429 } catch (IOException e
) {
1430 System
.out
.println("Failed to delete temp file “" + tmpFile
+ "”: " + e
.getMessage());
1436 private InputStream
retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer
, File tmpFile
) throws IOException
, InvalidMessageException
{
1437 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(serviceUrls
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
1438 return messageReceiver
.retrieveAttachment(pointer
, tmpFile
, MAX_ATTACHMENT_SIZE
);
1441 private String
canonicalizeNumber(String number
) throws InvalidNumberException
{
1442 String localNumber
= username
;
1443 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
1446 private SignalServiceAddress
getPushAddress(String number
) throws InvalidNumberException
{
1447 String e164number
= canonicalizeNumber(number
);
1448 return new SignalServiceAddress(e164number
);
1452 public boolean isRemote() {
1456 private void sendGroups() throws IOException
, UntrustedIdentityException
{
1457 File groupsFile
= Util
.createTempFile();
1460 try (OutputStream fos
= new FileOutputStream(groupsFile
)) {
1461 DeviceGroupsOutputStream out
= new DeviceGroupsOutputStream(fos
);
1462 for (GroupInfo
record : groupStore
.getGroups()) {
1463 out
.write(new DeviceGroup(record.groupId
, Optional
.fromNullable(record.name
),
1464 new ArrayList
<>(record.members
), createGroupAvatarAttachment(record.groupId
),
1469 if (groupsFile
.exists() && groupsFile
.length() > 0) {
1470 try (FileInputStream groupsFileStream
= new FileInputStream(groupsFile
)) {
1471 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1472 .withStream(groupsFileStream
)
1473 .withContentType("application/octet-stream")
1474 .withLength(groupsFile
.length())
1477 sendSyncMessage(SignalServiceSyncMessage
.forGroups(attachmentStream
));
1482 Files
.delete(groupsFile
.toPath());
1483 } catch (IOException e
) {
1484 System
.out
.println("Failed to delete temp file “" + groupsFile
+ "”: " + e
.getMessage());
1489 private void sendContacts() throws IOException
, UntrustedIdentityException
{
1490 File contactsFile
= Util
.createTempFile();
1493 try (OutputStream fos
= new FileOutputStream(contactsFile
)) {
1494 DeviceContactsOutputStream out
= new DeviceContactsOutputStream(fos
);
1495 for (ContactInfo
record : contactStore
.getContacts()) {
1496 out
.write(new DeviceContact(record.number
, Optional
.fromNullable(record.name
),
1497 createContactAvatarAttachment(record.number
), Optional
.fromNullable(record.color
)));
1501 if (contactsFile
.exists() && contactsFile
.length() > 0) {
1502 try (FileInputStream contactsFileStream
= new FileInputStream(contactsFile
)) {
1503 SignalServiceAttachmentStream attachmentStream
= SignalServiceAttachment
.newStreamBuilder()
1504 .withStream(contactsFileStream
)
1505 .withContentType("application/octet-stream")
1506 .withLength(contactsFile
.length())
1509 sendSyncMessage(SignalServiceSyncMessage
.forContacts(attachmentStream
));
1514 Files
.delete(contactsFile
.toPath());
1515 } catch (IOException e
) {
1516 System
.out
.println("Failed to delete temp file “" + contactsFile
+ "”: " + e
.getMessage());
1521 public ContactInfo
getContact(String number
) {
1522 return contactStore
.getContact(number
);
1525 public GroupInfo
getGroup(byte[] groupId
) {
1526 return groupStore
.getGroup(groupId
);
1529 public Map
<String
, List
<JsonIdentityKeyStore
.Identity
>> getIdentities() {
1530 return signalProtocolStore
.getIdentities();
1533 public List
<JsonIdentityKeyStore
.Identity
> getIdentities(String number
) {
1534 return signalProtocolStore
.getIdentities(number
);
1538 * Trust this the identity with this fingerprint
1540 * @param name username of the identity
1541 * @param fingerprint Fingerprint
1543 public boolean trustIdentityVerified(String name
, byte[] fingerprint
) {
1544 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1548 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1549 if (!Arrays
.equals(id
.getIdentityKey().serialize(), fingerprint
)) {
1553 signalProtocolStore
.saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1561 * Trust this the identity with this safety number
1563 * @param name username of the identity
1564 * @param safetyNumber Safety number
1566 public boolean trustIdentityVerifiedSafetyNumber(String name
, String safetyNumber
) {
1567 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1571 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1572 if (!safetyNumber
.equals(computeSafetyNumber(name
, id
.getIdentityKey()))) {
1576 signalProtocolStore
.saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_VERIFIED
);
1584 * Trust all keys of this identity without verification
1586 * @param name username of the identity
1588 public boolean trustIdentityAllKeys(String name
) {
1589 List
<JsonIdentityKeyStore
.Identity
> ids
= signalProtocolStore
.getIdentities(name
);
1593 for (JsonIdentityKeyStore
.Identity id
: ids
) {
1594 if (id
.getTrustLevel() == TrustLevel
.UNTRUSTED
) {
1595 signalProtocolStore
.saveIdentity(name
, id
.getIdentityKey(), TrustLevel
.TRUSTED_UNVERIFIED
);
1602 public String
computeSafetyNumber(String theirUsername
, IdentityKey theirIdentityKey
) {
1603 Fingerprint fingerprint
= new NumericFingerprintGenerator(5200).createFor(username
, getIdentity(), theirUsername
, theirIdentityKey
);
1604 return fingerprint
.getDisplayableFingerprint().getDisplayText();