]> nmode's Git Repositories - signal-cli/blob - src/main/java/cli/JsonSessionStore.java
Add possibility to send messages via dbus daemon
[signal-cli] / src / main / java / cli / JsonSessionStore.java
1 package cli;
2
3 import com.fasterxml.jackson.core.JsonGenerator;
4 import com.fasterxml.jackson.core.JsonParser;
5 import com.fasterxml.jackson.core.JsonProcessingException;
6 import com.fasterxml.jackson.databind.*;
7 import org.whispersystems.libaxolotl.AxolotlAddress;
8 import org.whispersystems.libaxolotl.state.SessionRecord;
9 import org.whispersystems.libaxolotl.state.SessionStore;
10
11 import java.io.IOException;
12 import java.util.*;
13
14 class JsonSessionStore implements SessionStore {
15
16 private final Map<AxolotlAddress, byte[]> sessions = new HashMap<>();
17
18 public JsonSessionStore() {
19
20 }
21
22 public void addSessions(Map<AxolotlAddress, byte[]> sessions) {
23 this.sessions.putAll(sessions);
24 }
25
26
27 @Override
28 public synchronized SessionRecord loadSession(AxolotlAddress remoteAddress) {
29 try {
30 if (containsSession(remoteAddress)) {
31 return new SessionRecord(sessions.get(remoteAddress));
32 } else {
33 return new SessionRecord();
34 }
35 } catch (IOException e) {
36 throw new AssertionError(e);
37 }
38 }
39
40 @Override
41 public synchronized List<Integer> getSubDeviceSessions(String name) {
42 List<Integer> deviceIds = new LinkedList<>();
43
44 for (AxolotlAddress key : sessions.keySet()) {
45 if (key.getName().equals(name) &&
46 key.getDeviceId() != 1) {
47 deviceIds.add(key.getDeviceId());
48 }
49 }
50
51 return deviceIds;
52 }
53
54 @Override
55 public synchronized void storeSession(AxolotlAddress address, SessionRecord record) {
56 sessions.put(address, record.serialize());
57 }
58
59 @Override
60 public synchronized boolean containsSession(AxolotlAddress address) {
61 return sessions.containsKey(address);
62 }
63
64 @Override
65 public synchronized void deleteSession(AxolotlAddress address) {
66 sessions.remove(address);
67 }
68
69 @Override
70 public synchronized void deleteAllSessions(String name) {
71 for (AxolotlAddress key : new ArrayList<>(sessions.keySet())) {
72 if (key.getName().equals(name)) {
73 sessions.remove(key);
74 }
75 }
76 }
77
78 public static class JsonSessionStoreDeserializer extends JsonDeserializer<JsonSessionStore> {
79
80 @Override
81 public JsonSessionStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
82 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
83
84 Map<AxolotlAddress, byte[]> sessionMap = new HashMap<>();
85 if (node.isArray()) {
86 for (JsonNode session : node) {
87 String sessionName = session.get("name").asText();
88 try {
89 sessionMap.put(new AxolotlAddress(sessionName, session.get("deviceId").asInt()), Base64.decode(session.get("record").asText()));
90 } catch (IOException e) {
91 System.out.println(String.format("Error while decoding session for: %s", sessionName));
92 }
93 }
94 }
95
96 JsonSessionStore sessionStore = new JsonSessionStore();
97 sessionStore.addSessions(sessionMap);
98
99 return sessionStore;
100
101 }
102 }
103
104 public static class JsonPreKeyStoreSerializer extends JsonSerializer<JsonSessionStore> {
105
106 @Override
107 public void serialize(JsonSessionStore jsonSessionStore, JsonGenerator json, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
108 json.writeStartArray();
109 for (Map.Entry<AxolotlAddress, byte[]> preKey : jsonSessionStore.sessions.entrySet()) {
110 json.writeStartObject();
111 json.writeStringField("name", preKey.getKey().getName());
112 json.writeNumberField("deviceId", preKey.getKey().getDeviceId());
113 json.writeStringField("record", Base64.encodeBytes(preKey.getValue()));
114 json.writeEndObject();
115 }
116 json.writeEndArray();
117 }
118 }
119 }