]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/Manager.java
1e7f02266554e6b5defdbb183c86e07f1035beac
[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 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);
88 }
89
90 public void save() {
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();
96 try {
97 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getFileName()));
98 writer.write(out);
99 writer.flush();
100 writer.close();
101 } catch (Exception e) {
102 System.out.println("Saving file error: " + e.getMessage());
103 return;
104 }
105 }
106
107 public void createNewIdentity() {
108 IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
109 int registrationId = KeyHelper.generateRegistrationId(false);
110 axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
111 registered = false;
112 }
113
114 public boolean isRegistered() {
115 return registered;
116 }
117
118 public void register() throws IOException {
119 password = Util.getSecret(18);
120
121 accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password);
122
123 accountManager.requestSmsVerificationCode();
124 registered = false;
125 }
126
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());
131
132 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
133 registered = true;
134 int start = 0;
135 List<PreKeyRecord> oneTimePreKeys = KeyHelper.generatePreKeys(start, 100);
136 PreKeyRecord lastResortKey = KeyHelper.generateLastResortPreKey();
137 int signedPreKeyId = 0;
138 SignedPreKeyRecord signedPreKeyRecord;
139 try {
140 signedPreKeyRecord = KeyHelper.generateSignedPreKey(axolotlStore.getIdentityKeyPair(), signedPreKeyId);
141 } catch (InvalidKeyException e) {
142 // Should really not happen
143 System.out.println("invalid key");
144 return;
145 }
146 accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
147 }
148
149 public TextSecureMessageSender getMessageSender() {
150 return new TextSecureMessageSender(URL, TRUST_STORE, username, password,
151 axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
152 }
153
154 public TextSecureContent receiveMessage() throws IOException, InvalidVersionException {
155 TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey);
156 TextSecureMessagePipe messagePipe = null;
157
158 try {
159 messagePipe = messageReceiver.createMessagePipe();
160
161 TextSecureEnvelope envelope;
162 try {
163 envelope = messagePipe.read(5, TimeUnit.SECONDS);
164 } catch (TimeoutException e) {
165 return null;
166 }
167 TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
168 TextSecureContent message = null;
169 try {
170 message = cipher.decrypt(envelope);
171 } catch (Exception e) {
172 // TODO handle all exceptions
173 e.printStackTrace();
174 }
175 return message;
176 } finally {
177 if (messagePipe != null)
178 messagePipe.shutdown();
179 }
180 }
181 }