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