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/>.
19 import org
.apache
.commons
.io
.IOUtils
;
20 import org
.json
.JSONObject
;
21 import org
.whispersystems
.libaxolotl
.IdentityKeyPair
;
22 import org
.whispersystems
.libaxolotl
.InvalidKeyException
;
23 import org
.whispersystems
.libaxolotl
.InvalidVersionException
;
24 import org
.whispersystems
.libaxolotl
.state
.PreKeyRecord
;
25 import org
.whispersystems
.libaxolotl
.state
.SignedPreKeyRecord
;
26 import org
.whispersystems
.libaxolotl
.util
.KeyHelper
;
27 import org
.whispersystems
.libaxolotl
.util
.guava
.Optional
;
28 import org
.whispersystems
.textsecure
.api
.TextSecureAccountManager
;
29 import org
.whispersystems
.textsecure
.api
.TextSecureMessagePipe
;
30 import org
.whispersystems
.textsecure
.api
.TextSecureMessageReceiver
;
31 import org
.whispersystems
.textsecure
.api
.TextSecureMessageSender
;
32 import org
.whispersystems
.textsecure
.api
.crypto
.TextSecureCipher
;
33 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureContent
;
34 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureEnvelope
;
35 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
36 import org
.whispersystems
.textsecure
.api
.push
.TrustStore
;
39 import java
.util
.List
;
40 import java
.util
.concurrent
.TimeUnit
;
41 import java
.util
.concurrent
.TimeoutException
;
43 public class Manager
{
44 private final static String URL
= "https://textsecure-service.whispersystems.org";
45 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
47 private final static String settingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
49 private String username
;
50 private String password
;
51 private String signalingKey
;
53 private boolean registered
= false;
55 private JsonAxolotlStore axolotlStore
;
56 TextSecureAccountManager accountManager
;
58 public Manager(String username
) {
59 this.username
= username
;
62 private String
getFileName() {
63 String path
= settingsPath
+ "/data";
64 new File(path
).mkdirs();
65 return path
+ "/" + username
;
68 public boolean userExists() {
69 File f
= new File(getFileName());
70 if (!f
.exists() || f
.isDirectory()) {
76 public boolean userHasKeys() {
77 return axolotlStore
!= null;
80 public void load() throws IOException
, InvalidKeyException
{
81 JSONObject
in = new JSONObject(IOUtils
.toString(new FileInputStream(getFileName())));
82 username
= in.getString("username");
83 password
= in.getString("password");
84 signalingKey
= in.getString("signalingKey");
85 axolotlStore
= new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
86 registered
= in.getBoolean("registered");
87 accountManager
= new TextSecureAccountManager(URL
, TRUST_STORE
, username
, password
);
91 String out
= new JSONObject().put("username", username
)
92 .put("password", password
)
93 .put("signalingKey", signalingKey
)
94 .put("axolotlStore", axolotlStore
.getJson())
95 .put("registered", registered
).toString();
97 OutputStreamWriter writer
= new OutputStreamWriter(new FileOutputStream(getFileName()));
101 } catch (Exception e
) {
102 System
.out
.println("Saving file error: " + e
.getMessage());
107 public void createNewIdentity() {
108 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
109 int registrationId
= KeyHelper
.generateRegistrationId(false);
110 axolotlStore
= new JsonAxolotlStore(identityKey
, registrationId
);
114 public boolean isRegistered() {
118 public void register() throws IOException
{
119 password
= Util
.getSecret(18);
121 accountManager
= new TextSecureAccountManager(URL
, TRUST_STORE
, username
, password
);
123 accountManager
.requestSmsVerificationCode();
127 public void verifyAccount(String verificationCode
) throws IOException
{
128 verificationCode
= verificationCode
.replace("-", "");
129 signalingKey
= Util
.getSecret(52);
130 accountManager
.verifyAccount(verificationCode
, signalingKey
, false, axolotlStore
.getLocalRegistrationId());
132 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
135 List
<PreKeyRecord
> oneTimePreKeys
= KeyHelper
.generatePreKeys(start
, 100);
136 PreKeyRecord lastResortKey
= KeyHelper
.generateLastResortPreKey();
137 int signedPreKeyId
= 0;
138 SignedPreKeyRecord signedPreKeyRecord
;
140 signedPreKeyRecord
= KeyHelper
.generateSignedPreKey(axolotlStore
.getIdentityKeyPair(), signedPreKeyId
);
141 } catch (InvalidKeyException e
) {
142 // Should really not happen
143 System
.out
.println("invalid key");
146 accountManager
.setPreKeys(axolotlStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
149 public TextSecureMessageSender
getMessageSender() {
150 return new TextSecureMessageSender(URL
, TRUST_STORE
, username
, password
,
151 axolotlStore
, Optional
.<TextSecureMessageSender
.EventListener
>absent());
154 public TextSecureContent
receiveMessage() throws IOException
, InvalidVersionException
{
155 TextSecureMessageReceiver messageReceiver
= new TextSecureMessageReceiver(URL
, TRUST_STORE
, username
, password
, signalingKey
);
156 TextSecureMessagePipe messagePipe
= null;
159 messagePipe
= messageReceiver
.createMessagePipe();
161 TextSecureEnvelope envelope
;
163 envelope
= messagePipe
.read(5, TimeUnit
.SECONDS
);
164 } catch (TimeoutException e
) {
167 TextSecureCipher cipher
= new TextSecureCipher(new TextSecureAddress(username
), axolotlStore
);
168 TextSecureContent message
= null;
170 message
= cipher
.decrypt(envelope
);
171 } catch (Exception e
) {
172 // TODO handle all exceptions
177 if (messagePipe
!= null)
178 messagePipe
.shutdown();