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
.databind
.DeserializationFeature
;
22 import com
.fasterxml
.jackson
.databind
.JsonNode
;
23 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
24 import com
.fasterxml
.jackson
.databind
.SerializationFeature
;
25 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
26 import org
.asamk
.Signal
;
27 import org
.whispersystems
.libsignal
.*;
28 import org
.whispersystems
.libsignal
.ecc
.Curve
;
29 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
30 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
31 import org
.whispersystems
.libsignal
.state
.SignalProtocolStore
;
32 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
33 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
34 import org
.whispersystems
.libsignal
.util
.Medium
;
35 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
36 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
37 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
38 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
39 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
40 import org
.whispersystems
.signalservice
.api
.crypto
.*;
41 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
42 import org
.whispersystems
.signalservice
.api
.messages
.*;
43 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
44 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
45 import org
.whispersystems
.signalservice
.api
.push
.TrustStore
;
46 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
47 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
48 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
49 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
53 import java
.net
.URISyntaxException
;
54 import java
.net
.URLEncoder
;
55 import java
.nio
.file
.Files
;
56 import java
.nio
.file
.Paths
;
58 import java
.util
.concurrent
.TimeUnit
;
59 import java
.util
.concurrent
.TimeoutException
;
61 class Manager
implements Signal
{
62 private final static String URL
= "https://textsecure-service.whispersystems.org";
63 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
65 public final static String PROJECT_NAME
= Manager
.class.getPackage().getImplementationTitle();
66 public final static String PROJECT_VERSION
= Manager
.class.getPackage().getImplementationVersion();
67 private final static String USER_AGENT
= PROJECT_NAME
== null ?
null : PROJECT_NAME
+ " " + PROJECT_VERSION
;
69 private final static int PREKEY_MINIMUM_COUNT
= 20;
70 private static final int PREKEY_BATCH_SIZE
= 100;
72 private final String settingsPath
;
73 private final String dataPath
;
74 private final String attachmentsPath
;
76 private final ObjectMapper jsonProcessot
= new ObjectMapper();
77 private String username
;
78 int deviceId
= SignalServiceAddress
.DEFAULT_DEVICE_ID
;
79 private String password
;
80 private String signalingKey
;
81 private int preKeyIdOffset
;
82 private int nextSignedPreKeyId
;
84 private boolean registered
= false;
86 private SignalProtocolStore signalProtocolStore
;
87 private SignalServiceAccountManager accountManager
;
88 private JsonGroupStore groupStore
;
90 public Manager(String username
, String settingsPath
) {
91 this.username
= username
;
92 this.settingsPath
= settingsPath
;
93 this.dataPath
= this.settingsPath
+ "/data";
94 this.attachmentsPath
= this.settingsPath
+ "/attachments";
96 jsonProcessot
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
97 jsonProcessot
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
98 jsonProcessot
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
99 jsonProcessot
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
102 public String
getUsername() {
106 public String
getFileName() {
107 new File(dataPath
).mkdirs();
108 return dataPath
+ "/" + username
;
111 public boolean userExists() {
112 if (username
== null) {
115 File f
= new File(getFileName());
116 return !(!f
.exists() || f
.isDirectory());
119 public boolean userHasKeys() {
120 return signalProtocolStore
!= null;
123 private JsonNode
getNotNullNode(JsonNode parent
, String name
) throws InvalidObjectException
{
124 JsonNode node
= parent
.get(name
);
126 throw new InvalidObjectException(String
.format("Incorrect file format: expected parameter %s not found ", name
));
132 public void load() throws IOException
, InvalidKeyException
{
133 JsonNode rootNode
= jsonProcessot
.readTree(new File(getFileName()));
135 JsonNode node
= rootNode
.get("deviceId");
137 deviceId
= node
.asInt();
139 username
= getNotNullNode(rootNode
, "username").asText();
140 password
= getNotNullNode(rootNode
, "password").asText();
141 if (rootNode
.has("signalingKey")) {
142 signalingKey
= getNotNullNode(rootNode
, "signalingKey").asText();
144 if (rootNode
.has("preKeyIdOffset")) {
145 preKeyIdOffset
= getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
149 if (rootNode
.has("nextSignedPreKeyId")) {
150 nextSignedPreKeyId
= getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
152 nextSignedPreKeyId
= 0;
154 signalProtocolStore
= jsonProcessot
.convertValue(getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
155 registered
= getNotNullNode(rootNode
, "registered").asBoolean();
156 JsonNode groupStoreNode
= rootNode
.get("groupStore");
157 if (groupStoreNode
!= null) {
158 groupStore
= jsonProcessot
.convertValue(groupStoreNode
, JsonGroupStore
.class);
160 if (groupStore
== null) {
161 groupStore
= new JsonGroupStore();
163 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, deviceId
, USER_AGENT
);
165 if (registered
&& accountManager
.getPreKeysCount() < PREKEY_MINIMUM_COUNT
) {
169 } catch (AuthorizationFailedException e
) {
170 System
.err
.println("Authorization failed, was the number registered elsewhere?");
174 private void save() {
175 ObjectNode rootNode
= jsonProcessot
.createObjectNode();
176 rootNode
.put("username", username
)
177 .put("deviceId", deviceId
)
178 .put("password", password
)
179 .put("signalingKey", signalingKey
)
180 .put("preKeyIdOffset", preKeyIdOffset
)
181 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
182 .put("registered", registered
)
183 .putPOJO("axolotlStore", signalProtocolStore
)
184 .putPOJO("groupStore", groupStore
)
187 jsonProcessot
.writeValue(new File(getFileName()), rootNode
);
188 } catch (Exception e
) {
189 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
193 public void createNewIdentity() {
194 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
195 int registrationId
= KeyHelper
.generateRegistrationId(false);
196 signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
197 groupStore
= new JsonGroupStore();
202 public boolean isRegistered() {
206 public void register(boolean voiceVerication
) throws IOException
{
207 password
= Util
.getSecret(18);
209 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, USER_AGENT
);
212 accountManager
.requestVoiceVerificationCode();
214 accountManager
.requestSmsVerificationCode();
220 public URI
getDeviceLinkUri() throws TimeoutException
, IOException
{
221 password
= Util
.getSecret(18);
223 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, USER_AGENT
);
224 String uuid
= accountManager
.getNewDeviceUuid();
228 return new URI("tsdevice:/?uuid=" + URLEncoder
.encode(uuid
, "utf-8") + "&pub_key=" + URLEncoder
.encode(Base64
.encodeBytesWithoutPadding(signalProtocolStore
.getIdentityKeyPair().getPublicKey().serialize()), "utf-8"));
229 } catch (URISyntaxException e
) {
235 public void finishDeviceLink(String deviceName
) throws IOException
, InvalidKeyException
, TimeoutException
, UserAlreadyExists
{
236 signalingKey
= Util
.getSecret(52);
237 SignalServiceAccountManager
.NewDeviceRegistrationReturn ret
= accountManager
.finishNewDeviceRegistration(signalProtocolStore
.getIdentityKeyPair(), signalingKey
, false, true, signalProtocolStore
.getLocalRegistrationId(), deviceName
);
238 deviceId
= ret
.getDeviceId();
239 username
= ret
.getNumber();
240 // TODO do this check before actually registering
242 throw new UserAlreadyExists(username
, getFileName());
244 signalProtocolStore
= new JsonSignalProtocolStore(ret
.getIdentity(), signalProtocolStore
.getLocalRegistrationId());
250 private List
<PreKeyRecord
> generatePreKeys() {
251 List
<PreKeyRecord
> records
= new LinkedList
<>();
253 for (int i
= 0; i
< PREKEY_BATCH_SIZE
; i
++) {
254 int preKeyId
= (preKeyIdOffset
+ i
) % Medium
.MAX_VALUE
;
255 ECKeyPair keyPair
= Curve
.generateKeyPair();
256 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
258 signalProtocolStore
.storePreKey(preKeyId
, record);
262 preKeyIdOffset
= (preKeyIdOffset
+ PREKEY_BATCH_SIZE
+ 1) % Medium
.MAX_VALUE
;
268 private PreKeyRecord
getOrGenerateLastResortPreKey() {
269 if (signalProtocolStore
.containsPreKey(Medium
.MAX_VALUE
)) {
271 return signalProtocolStore
.loadPreKey(Medium
.MAX_VALUE
);
272 } catch (InvalidKeyIdException e
) {
273 signalProtocolStore
.removePreKey(Medium
.MAX_VALUE
);
277 ECKeyPair keyPair
= Curve
.generateKeyPair();
278 PreKeyRecord
record = new PreKeyRecord(Medium
.MAX_VALUE
, keyPair
);
280 signalProtocolStore
.storePreKey(Medium
.MAX_VALUE
, record);
286 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
288 ECKeyPair keyPair
= Curve
.generateKeyPair();
289 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
290 SignedPreKeyRecord
record = new SignedPreKeyRecord(nextSignedPreKeyId
, System
.currentTimeMillis(), keyPair
, signature
);
292 signalProtocolStore
.storeSignedPreKey(nextSignedPreKeyId
, record);
293 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
297 } catch (InvalidKeyException e
) {
298 throw new AssertionError(e
);
302 public void verifyAccount(String verificationCode
) throws IOException
{
303 verificationCode
= verificationCode
.replace("-", "");
304 signalingKey
= Util
.getSecret(52);
305 accountManager
.verifyAccountWithCode(verificationCode
, signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, true);
307 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
314 private void refreshPreKeys() throws IOException
{
315 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
316 PreKeyRecord lastResortKey
= getOrGenerateLastResortPreKey();
317 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(signalProtocolStore
.getIdentityKeyPair());
319 accountManager
.setPreKeys(signalProtocolStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
323 private static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
324 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
325 if (attachments
!= null) {
326 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
327 for (String attachment
: attachments
) {
329 SignalServiceAttachments
.add(createAttachment(attachment
));
330 } catch (IOException e
) {
331 throw new AttachmentInvalidException(attachment
, e
);
335 return SignalServiceAttachments
;
338 private static SignalServiceAttachmentStream
createAttachment(String attachment
) throws IOException
{
339 File attachmentFile
= new File(attachment
);
340 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
341 final long attachmentSize
= attachmentFile
.length();
342 String mime
= Files
.probeContentType(Paths
.get(attachment
));
343 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, null);
347 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
349 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
, UntrustedIdentityException
{
350 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
351 if (attachments
!= null) {
352 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
354 if (groupId
!= null) {
355 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
358 messageBuilder
.asGroupMessage(group
);
360 SignalServiceDataMessage message
= messageBuilder
.build();
362 sendMessage(message
, groupStore
.getGroup(groupId
).members
);
365 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
, UntrustedIdentityException
{
366 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
370 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
371 .asGroupMessage(group
)
374 sendMessage(message
, groupStore
.getGroup(groupId
).members
);
377 public byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
, UntrustedIdentityException
{
379 if (groupId
== null) {
381 g
= new GroupInfo(Util
.getSecretBytes(16));
382 g
.members
.add(username
);
384 g
= groupStore
.getGroup(groupId
);
391 if (members
!= null) {
392 for (String member
: members
) {
394 g
.members
.add(canonicalizeNumber(member
));
395 } catch (InvalidNumberException e
) {
396 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
397 System
.err
.println("Aborting…");
403 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
406 .withMembers(new ArrayList
<>(g
.members
));
408 if (avatarFile
!= null) {
410 group
.withAvatar(createAttachment(avatarFile
));
413 } catch (IOException e
) {
414 throw new AttachmentInvalidException(avatarFile
, e
);
418 groupStore
.updateGroup(g
);
420 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
421 .asGroupMessage(group
.build())
424 sendMessage(message
, g
.members
);
429 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
430 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
, UntrustedIdentityException
{
431 List
<String
> recipients
= new ArrayList
<>(1);
432 recipients
.add(recipient
);
433 sendMessage(message
, attachments
, recipients
);
437 public void sendMessage(String messageText
, List
<String
> attachments
,
438 List
<String
> recipients
)
439 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
, UntrustedIdentityException
{
440 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
441 if (attachments
!= null) {
442 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
444 SignalServiceDataMessage message
= messageBuilder
.build();
446 sendMessage(message
, recipients
);
450 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
, UntrustedIdentityException
{
451 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
452 .asEndSessionMessage()
455 sendMessage(message
, recipients
);
458 private void sendMessage(SignalServiceDataMessage message
, Collection
<String
> recipients
)
459 throws IOException
, EncapsulatedExceptions
, UntrustedIdentityException
{
460 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(URL
, TRUST_STORE
, username
, password
,
461 deviceId
, signalProtocolStore
, USER_AGENT
, Optional
.<SignalServiceMessageSender
.EventListener
>absent());
463 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
464 for (String recipient
: recipients
) {
466 recipientsTS
.add(getPushAddress(recipient
));
467 } catch (InvalidNumberException e
) {
468 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
469 System
.err
.println("Aborting sending.");
475 if (message
.getGroupInfo().isPresent()) {
476 messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), message
);
478 // Send to all individually, so sync messages are sent correctly
479 for (SignalServiceAddress address
: recipientsTS
) {
480 messageSender
.sendMessage(address
, message
);
484 if (message
.isEndSession()) {
485 for (SignalServiceAddress recipient
: recipientsTS
) {
486 handleEndSession(recipient
.getNumber());
492 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) {
493 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), signalProtocolStore
);
495 return cipher
.decrypt(envelope
);
496 } catch (Exception e
) {
497 // TODO handle all exceptions
503 private void handleEndSession(String source
) {
504 signalProtocolStore
.deleteAllSessions(source
);
507 public interface ReceiveMessageHandler
{
508 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, GroupInfo group
);
511 private GroupInfo
handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
) {
512 GroupInfo group
= null;
513 if (message
.getGroupInfo().isPresent()) {
514 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
515 switch (groupInfo
.getType()) {
518 group
= groupStore
.getGroup(groupInfo
.getGroupId());
519 } catch (GroupNotFoundException e
) {
520 group
= new GroupInfo(groupInfo
.getGroupId());
523 if (groupInfo
.getAvatar().isPresent()) {
524 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
525 if (avatar
.isPointer()) {
526 long avatarId
= avatar
.asPointer().getId();
528 retrieveAttachment(avatar
.asPointer());
529 group
.avatarId
= avatarId
;
530 } catch (IOException
| InvalidMessageException e
) {
531 System
.err
.println("Failed to retrieve group avatar (" + avatarId
+ "): " + e
.getMessage());
536 if (groupInfo
.getName().isPresent()) {
537 group
.name
= groupInfo
.getName().get();
540 if (groupInfo
.getMembers().isPresent()) {
541 group
.members
.addAll(groupInfo
.getMembers().get());
544 groupStore
.updateGroup(group
);
548 group
= groupStore
.getGroup(groupInfo
.getGroupId());
549 } catch (GroupNotFoundException e
) {
554 group
= groupStore
.getGroup(groupInfo
.getGroupId());
555 group
.members
.remove(source
);
556 } catch (GroupNotFoundException e
) {
561 if (message
.isEndSession()) {
562 handleEndSession(isSync ? destination
: source
);
564 if (message
.getAttachments().isPresent()) {
565 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
566 if (attachment
.isPointer()) {
568 retrieveAttachment(attachment
.asPointer());
569 } catch (IOException
| InvalidMessageException e
) {
570 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
578 public void receiveMessages(int timeoutSeconds
, boolean returnOnTimeout
, ReceiveMessageHandler handler
) throws IOException
{
579 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
580 SignalServiceMessagePipe messagePipe
= null;
583 messagePipe
= messageReceiver
.createMessagePipe();
586 SignalServiceEnvelope envelope
;
587 SignalServiceContent content
= null;
588 GroupInfo group
= null;
590 envelope
= messagePipe
.read(timeoutSeconds
, TimeUnit
.SECONDS
);
591 if (!envelope
.isReceipt()) {
592 content
= decryptMessage(envelope
);
593 if (content
!= null) {
594 if (content
.getDataMessage().isPresent()) {
595 SignalServiceDataMessage message
= content
.getDataMessage().get();
596 group
= handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
);
598 if (content
.getSyncMessage().isPresent()) {
599 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
600 if (syncMessage
.getSent().isPresent()) {
601 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
602 group
= handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get());
604 if (syncMessage
.getRequest().isPresent()) {
607 if (syncMessage
.getGroups().isPresent()) {
614 handler
.handleMessage(envelope
, content
, group
);
615 } catch (TimeoutException e
) {
618 } catch (InvalidVersionException e
) {
619 System
.err
.println("Ignoring error: " + e
.getMessage());
623 if (messagePipe
!= null)
624 messagePipe
.shutdown();
628 public File
getAttachmentFile(long attachmentId
) {
629 return new File(attachmentsPath
, attachmentId
+ "");
632 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
633 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, deviceId
, signalingKey
, USER_AGENT
);
635 File tmpFile
= File
.createTempFile("ts_attach_" + pointer
.getId(), ".tmp");
636 InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
);
638 new File(attachmentsPath
).mkdirs();
639 File outputFile
= getAttachmentFile(pointer
.getId());
640 OutputStream output
= null;
642 output
= new FileOutputStream(outputFile
);
643 byte[] buffer
= new byte[4096];
646 while ((read
= input
.read(buffer
)) != -1) {
647 output
.write(buffer
, 0, read
);
649 } catch (FileNotFoundException e
) {
653 if (output
!= null) {
657 if (!tmpFile
.delete()) {
658 System
.err
.println("Failed to delete temp file: " + tmpFile
);
661 if (pointer
.getPreview().isPresent()) {
662 File previewFile
= new File(outputFile
+ ".preview");
664 output
= new FileOutputStream(previewFile
);
665 byte[] preview
= pointer
.getPreview().get();
666 output
.write(preview
, 0, preview
.length
);
667 } catch (FileNotFoundException e
) {
671 if (output
!= null) {
679 private String
canonicalizeNumber(String number
) throws InvalidNumberException
{
680 String localNumber
= username
;
681 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
684 private SignalServiceAddress
getPushAddress(String number
) throws InvalidNumberException
{
685 String e164number
= canonicalizeNumber(number
);
686 return new SignalServiceAddress(e164number
);
690 public boolean isRemote() {