]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/Manager.java
ff2178b416245c14e7e96ca282112f4fa69df661
[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() throws IOException {
121 password = Util.getSecret(18);
122
123 accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password);
124
125 accountManager.requestSmsVerificationCode();
126 registered = false;
127 }
128
129 public void verifyAccount(String verificationCode) throws IOException {
130 verificationCode = verificationCode.replace("-", "");
131 signalingKey = Util.getSecret(52);
132 accountManager.verifyAccount(verificationCode, signalingKey, false, axolotlStore.getLocalRegistrationId());
133
134 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
135 registered = true;
136 int start = 0;
137 List<PreKeyRecord> oneTimePreKeys = KeyHelper.generatePreKeys(start, 100);
138 PreKeyRecord lastResortKey = KeyHelper.generateLastResortPreKey();
139 int signedPreKeyId = 0;
140 SignedPreKeyRecord signedPreKeyRecord;
141 try {
142 signedPreKeyRecord = KeyHelper.generateSignedPreKey(axolotlStore.getIdentityKeyPair(), signedPreKeyId);
143 } catch (InvalidKeyException e) {
144 // Should really not happen
145 System.out.println("invalid key");
146 return;
147 }
148 accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
149 }
150
151 public TextSecureMessageSender getMessageSender() {
152 return new TextSecureMessageSender(URL, TRUST_STORE, username, password,
153 axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
154 }
155
156 public TextSecureContent receiveMessage() throws IOException, InvalidVersionException {
157 TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey);
158 TextSecureMessagePipe messagePipe = null;
159
160 try {
161 messagePipe = messageReceiver.createMessagePipe();
162
163 TextSecureEnvelope envelope;
164 try {
165 envelope = messagePipe.read(5, TimeUnit.SECONDS);
166 } catch (TimeoutException e) {
167 return null;
168 }
169 TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
170 TextSecureContent message = null;
171 try {
172 message = cipher.decrypt(envelope);
173 } catch (Exception e) {
174 // TODO handle all exceptions
175 e.printStackTrace();
176 }
177 return message;
178 } finally {
179 if (messagePipe != null)
180 messagePipe.shutdown();
181 }
182 }
183 }