X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/ab250030619b2b470d62ce60f462d9b43f5bbab2..2517919c49ccf3ed3c4bfa90782a4afa54fbf547:/src/main/java/cli/Manager.java diff --git a/src/main/java/cli/Manager.java b/src/main/java/cli/Manager.java index 846e0dc9..758dabc3 100644 --- a/src/main/java/cli/Manager.java +++ b/src/main/java/cli/Manager.java @@ -16,8 +16,13 @@ */ package cli; -import org.apache.commons.io.IOUtils; -import org.json.JSONObject; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.whispersystems.libaxolotl.*; import org.whispersystems.libaxolotl.ecc.Curve; import org.whispersystems.libaxolotl.ecc.ECKeyPair; @@ -31,10 +36,7 @@ import org.whispersystems.textsecure.api.TextSecureMessagePipe; import org.whispersystems.textsecure.api.TextSecureMessageReceiver; import org.whispersystems.textsecure.api.TextSecureMessageSender; import org.whispersystems.textsecure.api.crypto.TextSecureCipher; -import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer; -import org.whispersystems.textsecure.api.messages.TextSecureContent; -import org.whispersystems.textsecure.api.messages.TextSecureDataMessage; -import org.whispersystems.textsecure.api.messages.TextSecureEnvelope; +import org.whispersystems.textsecure.api.messages.*; import org.whispersystems.textsecure.api.push.TextSecureAddress; import org.whispersystems.textsecure.api.push.TrustStore; import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions; @@ -51,14 +53,15 @@ class Manager { private final static String URL = "https://textsecure-service.whispersystems.org"; private final static TrustStore TRUST_STORE = new WhisperTrustStore(); - public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle(); - public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion(); + public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle(); + public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion(); private final static String USER_AGENT = PROJECT_NAME + " " + PROJECT_VERSION; private final static String settingsPath = System.getProperty("user.home") + "/.config/textsecure"; private final static String dataPath = settingsPath + "/data"; private final static String attachmentsPath = settingsPath + "/attachments"; + private final ObjectMapper jsonProcessot = new ObjectMapper(); private String username; private String password; private String signalingKey; @@ -69,9 +72,14 @@ class Manager { private JsonAxolotlStore axolotlStore; private TextSecureAccountManager accountManager; + private JsonGroupStore groupStore; public Manager(String username) { this.username = username; + jsonProcessot.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); // disable autodetect + jsonProcessot.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print, you can disable it. + jsonProcessot.enable(SerializationFeature.WRITE_NULL_MAP_VALUES); + jsonProcessot.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); } public String getFileName() { @@ -88,43 +96,57 @@ class Manager { return axolotlStore != null; } + private JsonNode getNotNullNode(JsonNode parent, String name) throws InvalidObjectException { + JsonNode node = parent.get(name); + if (node == null) { + throw new InvalidObjectException(String.format("Incorrect file format: expected parameter %s not found ", name)); + } + + return node; + } + public void load() throws IOException, InvalidKeyException { - JSONObject in = new JSONObject(IOUtils.toString(new FileInputStream(getFileName()))); - username = in.getString("username"); - password = in.getString("password"); - if (in.has("signalingKey")) { - signalingKey = in.getString("signalingKey"); + JsonNode rootNode = jsonProcessot.readTree(new File(getFileName())); + + username = getNotNullNode(rootNode, "username").asText(); + password = getNotNullNode(rootNode, "password").asText(); + if (rootNode.has("signalingKey")) { + signalingKey = getNotNullNode(rootNode, "signalingKey").asText(); } - if (in.has("preKeyIdOffset")) { - preKeyIdOffset = in.getInt("preKeyIdOffset"); + if (rootNode.has("preKeyIdOffset")) { + preKeyIdOffset = getNotNullNode(rootNode, "preKeyIdOffset").asInt(0); } else { preKeyIdOffset = 0; } - if (in.has("nextSignedPreKeyId")) { - nextSignedPreKeyId = in.getInt("nextSignedPreKeyId"); + if (rootNode.has("nextSignedPreKeyId")) { + nextSignedPreKeyId = getNotNullNode(rootNode, "nextSignedPreKeyId").asInt(); } else { nextSignedPreKeyId = 0; } - axolotlStore = new JsonAxolotlStore(in.getJSONObject("axolotlStore")); - registered = in.getBoolean("registered"); + axolotlStore = jsonProcessot.convertValue(getNotNullNode(rootNode, "axolotlStore"), JsonAxolotlStore.class); //new JsonAxolotlStore(in.getJSONObject("axolotlStore")); + registered = getNotNullNode(rootNode, "registered").asBoolean(); + JsonNode groupStoreNode = rootNode.get("groupStore"); + if (groupStoreNode != null) { + groupStore = jsonProcessot.convertValue(groupStoreNode, JsonGroupStore.class); + } accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password, USER_AGENT); } public void save() { - String out = new JSONObject().put("username", username) + ObjectNode rootNode = jsonProcessot.createObjectNode(); + rootNode.put("username", username) .put("password", password) .put("signalingKey", signalingKey) .put("preKeyIdOffset", preKeyIdOffset) .put("nextSignedPreKeyId", nextSignedPreKeyId) - .put("axolotlStore", axolotlStore.getJson()) - .put("registered", registered).toString(); + .put("registered", registered) + .putPOJO("axolotlStore", axolotlStore) + .putPOJO("groupStore", groupStore) + ; try { - OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getFileName())); - writer.write(out); - writer.flush(); - writer.close(); + jsonProcessot.writeValue(new File(getFileName()), rootNode); } catch (Exception e) { - System.err.println("Saving file error: " + e.getMessage()); + System.err.println(String.format("Error saving file: %s", e.getMessage())); } } @@ -132,6 +154,7 @@ class Manager { IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair(); int registrationId = KeyHelper.generateRegistrationId(false); axolotlStore = new JsonAxolotlStore(identityKey, registrationId); + groupStore = new JsonGroupStore(); registered = false; } @@ -242,7 +265,7 @@ class Manager { } public interface ReceiveMessageHandler { - void handleMessage(TextSecureEnvelope envelope); + void handleMessage(TextSecureEnvelope envelope, TextSecureContent decryptedContent, GroupInfo group); } public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException { @@ -254,9 +277,37 @@ class Manager { while (true) { TextSecureEnvelope envelope; + TextSecureContent content = null; + GroupInfo group = null; try { envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS); - handler.handleMessage(envelope); + if (!envelope.isReceipt()) { + content = decryptMessage(envelope); + if (content != null) { + if (content.getDataMessage().isPresent()) { + TextSecureDataMessage message = content.getDataMessage().get(); + if (message.getGroupInfo().isPresent()) { + TextSecureGroup groupInfo = message.getGroupInfo().get(); + switch (groupInfo.getType()) { + case UPDATE: + group = new GroupInfo(groupInfo.getGroupId(), groupInfo.getName().get(), groupInfo.getMembers().get(), groupInfo.getAvatar().get().asPointer().getId()); + groupStore.updateGroup(group); + break; + case DELIVER: + group = groupStore.getGroup(groupInfo.getGroupId()); + break; + case QUIT: + group = groupStore.getGroup(groupInfo.getGroupId()); + if (group != null) { + group.members.remove(envelope.getSource()); + } + break; + } + } + } + } + } + handler.handleMessage(envelope, content, group); } catch (TimeoutException e) { if (returnOnTimeout) return; @@ -327,4 +378,8 @@ class Manager { String e164number = canonicalizeNumber(number); return new TextSecureAddress(e164number); } + + GroupInfo getGroupInfo(byte[] groupId) { + return groupStore.getGroup(groupId); + } }