From: AsamK Date: Wed, 25 Nov 2015 12:41:08 +0000 (+0100) Subject: Store group info in json X-Git-Tag: v0.1.0~7 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/c41ac8e7a32e0eaa3ab8786be14da46f4c20a08a?ds=sidebyside Store group info in json --- diff --git a/src/main/java/cli/GroupInfo.java b/src/main/java/cli/GroupInfo.java new file mode 100644 index 00000000..f3060d08 --- /dev/null +++ b/src/main/java/cli/GroupInfo.java @@ -0,0 +1,28 @@ +package cli; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class GroupInfo { + @JsonProperty + public final byte[] groupId; + + @JsonProperty + public String name; + + @JsonProperty + public List members = new ArrayList<>(); + + @JsonProperty + public long avatarId; + + public GroupInfo(@JsonProperty("groupId") byte[] groupId, @JsonProperty("name") String name, @JsonProperty("members") Collection members, @JsonProperty("avatarId") long avatarId) { + this.groupId = groupId; + this.name = name; + this.members.addAll(members); + this.avatarId = avatarId; + } +} diff --git a/src/main/java/cli/JsonGroupStore.java b/src/main/java/cli/JsonGroupStore.java new file mode 100644 index 00000000..29f9abfe --- /dev/null +++ b/src/main/java/cli/JsonGroupStore.java @@ -0,0 +1,50 @@ +package cli; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class JsonGroupStore { + @JsonProperty("groups") + @JsonSerialize(using = JsonGroupStore.MapToListSerializer.class) + @JsonDeserialize(using = JsonGroupStore.GroupsDeserializer.class) + private Map groups = new HashMap<>(); + + private static final ObjectMapper jsonProcessot = new ObjectMapper(); + + void updateGroup(GroupInfo group) { + groups.put(Base64.encodeBytes(group.groupId), group); + } + + GroupInfo getGroup(byte[] groupId) { + return groups.get(Base64.encodeBytes(groupId)); + } + + public static class MapToListSerializer extends JsonSerializer> { + @Override + public void serialize(final Map value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException { + jgen.writeObject(value.values()); + } + } + + public static class GroupsDeserializer extends JsonDeserializer> { + @Override + public Map deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { + Map groups = new HashMap<>(); + JsonNode node = jsonParser.getCodec().readTree(jsonParser); + for (JsonNode n : node) { + GroupInfo g = jsonProcessot.treeToValue(n, GroupInfo.class); + groups.put(Base64.encodeBytes(g.groupId), g); + } + + return groups; + } + } +} diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 728ea35b..85ca227d 100644 --- a/src/main/java/cli/Main.java +++ b/src/main/java/cli/Main.java @@ -262,15 +262,13 @@ public class Main { } @Override - public void handleMessage(TextSecureEnvelope envelope) { + public void handleMessage(TextSecureEnvelope envelope, TextSecureContent content, GroupInfo group) { System.out.println("Envelope from: " + envelope.getSource()); System.out.println("Timestamp: " + envelope.getTimestamp()); if (envelope.isReceipt()) { System.out.println("Got receipt."); } else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) { - TextSecureContent content = m.decryptMessage(envelope); - if (content == null) { System.out.println("Failed to decrypt message."); } else { @@ -288,6 +286,10 @@ public class Main { System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId())); if (groupInfo.getName().isPresent()) { System.out.println(" Name: " + groupInfo.getName().get()); + } else if (group != null) { + System.out.println(" Name: " + group.name); + } else { + System.out.println(" Name: "); } System.out.println(" Type: " + groupInfo.getType()); if (groupInfo.getMembers().isPresent()) { diff --git a/src/main/java/cli/Manager.java b/src/main/java/cli/Manager.java index 7ca2c123..2258c99d 100644 --- a/src/main/java/cli/Manager.java +++ b/src/main/java/cli/Manager.java @@ -36,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; @@ -75,6 +72,7 @@ class Manager { private JsonAxolotlStore axolotlStore; private TextSecureAccountManager accountManager; + private JsonGroupStore groupStore; public Manager(String username) { this.username = username; @@ -107,7 +105,6 @@ class Manager { return node; } - public void load() throws IOException, InvalidKeyException { JsonNode rootNode = jsonProcessot.readTree(new File(getFileName())); @@ -128,6 +125,10 @@ class Manager { } 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); } @@ -140,6 +141,7 @@ class Manager { .put("nextSignedPreKeyId", nextSignedPreKeyId) .put("registered", registered) .putPOJO("axolotlStore", axolotlStore) + .putPOJO("groupStore", groupStore) ; try { jsonProcessot.writeValue(new File(getFileName()), rootNode); @@ -152,6 +154,7 @@ class Manager { IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair(); int registrationId = KeyHelper.generateRegistrationId(false); axolotlStore = new JsonAxolotlStore(identityKey, registrationId); + groupStore = new JsonGroupStore(); registered = false; } @@ -262,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 { @@ -274,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;