]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/Manager.java
Update README
[signal-cli] / src / main / java / cli / Manager.java
1 /**
2 * Copyright (C) 2015 AsamK
3 * <p>
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 * <p>
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 * <p>
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 cli;
18
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;
37
38 import java.io.*;
39 import java.util.List;
40 import java.util.concurrent.TimeUnit;
41 import java.util.concurrent.TimeoutException;
42
43 public class Manager {
44 private final static String URL = "https://textsecure-service.whispersystems.org";
45 private final static TrustStore TRUST_STORE = new WhisperTrustStore();
46
47 private final static String settingsPath = System.getProperty("user.home") + "/.config/textsecure";
48
49 private String username;
50 private String password;
51 private String signalingKey;
52
53 private boolean registered = false;
54
55 private JsonAxolotlStore axolotlStore;
56 TextSecureAccountManager accountManager;
57
58 public Manager(String username) {
59 this.username = username;
60 }
61
62 private String getFileName() {
63 String path = settingsPath + "/data";
64 new File(path).mkdirs();
65 return path + "/" + username;
66 }
67
68 public boolean userExists() {
69 File f = new File(getFileName());
70 if (!f.exists() || f.isDirectory()) {
71 return false;
72 }
73 return true;
74 }
75
76 public boolean userHasKeys() {
77 return axolotlStore != null;
78 }
79
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 if (in.has("signalingKey")) {
85 signalingKey = in.getString("signalingKey");
86 }
87 axolotlStore = new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
88 registered = in.getBoolean("registered");
89 accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password);
90 }
91
92 public void save() {
93 String out = new JSONObject().put("username", username)
94 .put("password", password)
95 .put("signalingKey", signalingKey)
96 .put("axolotlStore", axolotlStore.getJson())
97 .put("registered", registered).toString();
98 try {
99 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getFileName()));
100 writer.write(out);
101 writer.flush();
102 writer.close();
103 } catch (Exception e) {
104 System.out.println("Saving file error: " + e.getMessage());
105 return;
106 }
107 }
108
109 public void createNewIdentity() {
110 IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
111 int registrationId = KeyHelper.generateRegistrationId(false);
112 axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
113 registered = false;
114 }
115
116 public boolean isRegistered() {
117 return registered;
118 }
119
120 public void register(boolean voiceVerication) throws IOException {
121 password = Util.getSecret(18);
122
123 accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password);
124
125 if (voiceVerication)
126 accountManager.requestVoiceVerificationCode();
127 else
128 accountManager.requestSmsVerificationCode();
129
130 registered = false;
131 }
132
133 public void verifyAccount(String verificationCode) throws IOException {
134 verificationCode = verificationCode.replace("-", "");
135 signalingKey = Util.getSecret(52);
136 accountManager.verifyAccount(verificationCode, signalingKey, false, axolotlStore.getLocalRegistrationId());
137
138 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
139 registered = true;
140 int start = 0;
141 List<PreKeyRecord> oneTimePreKeys = KeyHelper.generatePreKeys(start, 100);
142 PreKeyRecord lastResortKey = KeyHelper.generateLastResortPreKey();
143 int signedPreKeyId = 0;
144 SignedPreKeyRecord signedPreKeyRecord;
145 try {
146 signedPreKeyRecord = KeyHelper.generateSignedPreKey(axolotlStore.getIdentityKeyPair(), signedPreKeyId);
147 } catch (InvalidKeyException e) {
148 // Should really not happen
149 System.out.println("invalid key");
150 return;
151 }
152 accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
153 }
154
155 public TextSecureMessageSender getMessageSender() {
156 return new TextSecureMessageSender(URL, TRUST_STORE, username, password,
157 axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
158 }
159
160 public TextSecureContent receiveMessage() throws IOException, InvalidVersionException {
161 TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey);
162 TextSecureMessagePipe messagePipe = null;
163
164 try {
165 messagePipe = messageReceiver.createMessagePipe();
166
167 TextSecureEnvelope envelope;
168 try {
169 envelope = messagePipe.read(5, TimeUnit.SECONDS);
170 } catch (TimeoutException e) {
171 return null;
172 }
173 TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
174 TextSecureContent message = null;
175 try {
176 message = cipher.decrypt(envelope);
177 } catch (Exception e) {
178 // TODO handle all exceptions
179 e.printStackTrace();
180 }
181 return message;
182 } finally {
183 if (messagePipe != null)
184 messagePipe.shutdown();
185 }
186 }
187 }