]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/util/MessageCacheUtils.java
Extract lib module
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / MessageCacheUtils.java
1 package org.asamk.signal.manager.util;
2
3 import org.whispersystems.libsignal.util.guava.Optional;
4 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
5 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
6 import org.whispersystems.signalservice.api.util.UuidUtil;
7
8 import java.io.DataInputStream;
9 import java.io.DataOutputStream;
10 import java.io.File;
11 import java.io.FileInputStream;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.util.UUID;
15
16 public class MessageCacheUtils {
17
18 public static SignalServiceEnvelope loadEnvelope(File file) throws IOException {
19 try (FileInputStream f = new FileInputStream(file)) {
20 DataInputStream in = new DataInputStream(f);
21 int version = in.readInt();
22 if (version > 4) {
23 return null;
24 }
25 int type = in.readInt();
26 String source = in.readUTF();
27 UUID sourceUuid = null;
28 if (version >= 3) {
29 sourceUuid = UuidUtil.parseOrNull(in.readUTF());
30 }
31 int sourceDevice = in.readInt();
32 if (version == 1) {
33 // read legacy relay field
34 in.readUTF();
35 }
36 long timestamp = in.readLong();
37 byte[] content = null;
38 int contentLen = in.readInt();
39 if (contentLen > 0) {
40 content = new byte[contentLen];
41 in.readFully(content);
42 }
43 byte[] legacyMessage = null;
44 int legacyMessageLen = in.readInt();
45 if (legacyMessageLen > 0) {
46 legacyMessage = new byte[legacyMessageLen];
47 in.readFully(legacyMessage);
48 }
49 long serverReceivedTimestamp = 0;
50 String uuid = null;
51 if (version >= 2) {
52 serverReceivedTimestamp = in.readLong();
53 uuid = in.readUTF();
54 if ("".equals(uuid)) {
55 uuid = null;
56 }
57 }
58 long serverDeliveredTimestamp = 0;
59 if (version >= 4) {
60 serverDeliveredTimestamp = in.readLong();
61 }
62 Optional<SignalServiceAddress> addressOptional = sourceUuid == null && source.isEmpty()
63 ? Optional.absent()
64 : Optional.of(new SignalServiceAddress(sourceUuid, source));
65 return new SignalServiceEnvelope(type,
66 addressOptional,
67 sourceDevice,
68 timestamp,
69 legacyMessage,
70 content,
71 serverReceivedTimestamp,
72 serverDeliveredTimestamp,
73 uuid);
74 }
75 }
76
77 public static void storeEnvelope(SignalServiceEnvelope envelope, File file) throws IOException {
78 try (FileOutputStream f = new FileOutputStream(file)) {
79 try (DataOutputStream out = new DataOutputStream(f)) {
80 out.writeInt(4); // version
81 out.writeInt(envelope.getType());
82 out.writeUTF(envelope.getSourceE164().isPresent() ? envelope.getSourceE164().get() : "");
83 out.writeUTF(envelope.getSourceUuid().isPresent() ? envelope.getSourceUuid().get() : "");
84 out.writeInt(envelope.getSourceDevice());
85 out.writeLong(envelope.getTimestamp());
86 if (envelope.hasContent()) {
87 out.writeInt(envelope.getContent().length);
88 out.write(envelope.getContent());
89 } else {
90 out.writeInt(0);
91 }
92 if (envelope.hasLegacyMessage()) {
93 out.writeInt(envelope.getLegacyMessage().length);
94 out.write(envelope.getLegacyMessage());
95 } else {
96 out.writeInt(0);
97 }
98 out.writeLong(envelope.getServerReceivedTimestamp());
99 String uuid = envelope.getUuid();
100 out.writeUTF(uuid == null ? "" : uuid);
101 out.writeLong(envelope.getServerDeliveredTimestamp());
102 }
103 }
104 }
105 }