mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
Store envelopes using envelope proto
This commit is contained in:
parent
f6aebb5917
commit
60ad582012
1 changed files with 75 additions and 86 deletions
|
@ -6,6 +6,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
|
||||||
import org.whispersystems.signalservice.api.push.ServiceId;
|
import org.whispersystems.signalservice.api.push.ServiceId;
|
||||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||||
import org.whispersystems.signalservice.api.util.UuidUtil;
|
import org.whispersystems.signalservice.api.util.UuidUtil;
|
||||||
|
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
|
@ -19,7 +20,7 @@ public class MessageCacheUtils {
|
||||||
|
|
||||||
private final static Logger logger = LoggerFactory.getLogger(MessageCacheUtils.class);
|
private final static Logger logger = LoggerFactory.getLogger(MessageCacheUtils.class);
|
||||||
|
|
||||||
final static int CURRENT_VERSION = 8;
|
final static int CURRENT_VERSION = 9;
|
||||||
|
|
||||||
public static SignalServiceEnvelope loadEnvelope(File file) throws IOException {
|
public static SignalServiceEnvelope loadEnvelope(File file) throws IOException {
|
||||||
try (var f = new FileInputStream(file)) {
|
try (var f = new FileInputStream(file)) {
|
||||||
|
@ -31,74 +32,80 @@ public class MessageCacheUtils {
|
||||||
// Unsupported envelope version
|
// Unsupported envelope version
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var type = in.readInt();
|
if (version >= 9) {
|
||||||
var source = in.readUTF();
|
final var serverReceivedTimestamp = in.readLong();
|
||||||
ServiceId sourceServiceId = null;
|
final var envelope = SignalServiceProtos.Envelope.parseFrom(in.readAllBytes());
|
||||||
if (version >= 3) {
|
return new SignalServiceEnvelope(envelope, serverReceivedTimestamp);
|
||||||
sourceServiceId = ServiceId.parseOrNull(in.readUTF());
|
} else {
|
||||||
}
|
var type = in.readInt();
|
||||||
var sourceDevice = in.readInt();
|
var source = in.readUTF();
|
||||||
if (version == 1) {
|
ServiceId sourceServiceId = null;
|
||||||
// read legacy relay field
|
if (version >= 3) {
|
||||||
in.readUTF();
|
sourceServiceId = ServiceId.parseOrNull(in.readUTF());
|
||||||
}
|
|
||||||
String destinationUuid = null;
|
|
||||||
if (version >= 5) {
|
|
||||||
destinationUuid = in.readUTF();
|
|
||||||
}
|
|
||||||
var timestamp = in.readLong();
|
|
||||||
byte[] content = null;
|
|
||||||
var contentLen = in.readInt();
|
|
||||||
if (contentLen > 0) {
|
|
||||||
content = new byte[contentLen];
|
|
||||||
in.readFully(content);
|
|
||||||
}
|
|
||||||
var legacyMessageLen = in.readInt();
|
|
||||||
if (legacyMessageLen > 0) {
|
|
||||||
byte[] legacyMessage = new byte[legacyMessageLen];
|
|
||||||
in.readFully(legacyMessage);
|
|
||||||
}
|
|
||||||
long serverReceivedTimestamp = 0;
|
|
||||||
String uuid = null;
|
|
||||||
if (version >= 2) {
|
|
||||||
serverReceivedTimestamp = in.readLong();
|
|
||||||
uuid = in.readUTF();
|
|
||||||
if ("".equals(uuid)) {
|
|
||||||
uuid = null;
|
|
||||||
}
|
}
|
||||||
|
var sourceDevice = in.readInt();
|
||||||
|
if (version == 1) {
|
||||||
|
// read legacy relay field
|
||||||
|
in.readUTF();
|
||||||
|
}
|
||||||
|
String destinationUuid = null;
|
||||||
|
if (version >= 5) {
|
||||||
|
destinationUuid = in.readUTF();
|
||||||
|
}
|
||||||
|
var timestamp = in.readLong();
|
||||||
|
byte[] content = null;
|
||||||
|
var contentLen = in.readInt();
|
||||||
|
if (contentLen > 0) {
|
||||||
|
content = new byte[contentLen];
|
||||||
|
in.readFully(content);
|
||||||
|
}
|
||||||
|
var legacyMessageLen = in.readInt();
|
||||||
|
if (legacyMessageLen > 0) {
|
||||||
|
byte[] legacyMessage = new byte[legacyMessageLen];
|
||||||
|
in.readFully(legacyMessage);
|
||||||
|
}
|
||||||
|
long serverReceivedTimestamp = 0;
|
||||||
|
String uuid = null;
|
||||||
|
if (version >= 2) {
|
||||||
|
serverReceivedTimestamp = in.readLong();
|
||||||
|
uuid = in.readUTF();
|
||||||
|
if ("".equals(uuid)) {
|
||||||
|
uuid = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long serverDeliveredTimestamp = 0;
|
||||||
|
if (version >= 4) {
|
||||||
|
serverDeliveredTimestamp = in.readLong();
|
||||||
|
}
|
||||||
|
boolean isUrgent = true;
|
||||||
|
if (version >= 6) {
|
||||||
|
isUrgent = in.readBoolean();
|
||||||
|
}
|
||||||
|
boolean isStory = true;
|
||||||
|
if (version >= 7) {
|
||||||
|
isStory = in.readBoolean();
|
||||||
|
}
|
||||||
|
String updatedPni = null;
|
||||||
|
if (version >= 8) {
|
||||||
|
updatedPni = in.readUTF();
|
||||||
|
}
|
||||||
|
Optional<SignalServiceAddress> addressOptional = sourceServiceId == null
|
||||||
|
? Optional.empty()
|
||||||
|
: Optional.of(new SignalServiceAddress(sourceServiceId, source));
|
||||||
|
return new SignalServiceEnvelope(type,
|
||||||
|
addressOptional,
|
||||||
|
sourceDevice,
|
||||||
|
timestamp,
|
||||||
|
content,
|
||||||
|
serverReceivedTimestamp,
|
||||||
|
serverDeliveredTimestamp,
|
||||||
|
uuid,
|
||||||
|
destinationUuid == null ? UuidUtil.UNKNOWN_UUID.toString() : destinationUuid,
|
||||||
|
isUrgent,
|
||||||
|
isStory,
|
||||||
|
null,
|
||||||
|
updatedPni == null ? "" : updatedPni);
|
||||||
}
|
}
|
||||||
long serverDeliveredTimestamp = 0;
|
|
||||||
if (version >= 4) {
|
|
||||||
serverDeliveredTimestamp = in.readLong();
|
|
||||||
}
|
|
||||||
boolean isUrgent = true;
|
|
||||||
if (version >= 6) {
|
|
||||||
isUrgent = in.readBoolean();
|
|
||||||
}
|
|
||||||
boolean isStory = true;
|
|
||||||
if (version >= 7) {
|
|
||||||
isStory = in.readBoolean();
|
|
||||||
}
|
|
||||||
String updatedPni = null;
|
|
||||||
if (version >= 8) {
|
|
||||||
updatedPni = in.readUTF();
|
|
||||||
}
|
|
||||||
Optional<SignalServiceAddress> addressOptional = sourceServiceId == null
|
|
||||||
? Optional.empty()
|
|
||||||
: Optional.of(new SignalServiceAddress(sourceServiceId, source));
|
|
||||||
return new SignalServiceEnvelope(type,
|
|
||||||
addressOptional,
|
|
||||||
sourceDevice,
|
|
||||||
timestamp,
|
|
||||||
content,
|
|
||||||
serverReceivedTimestamp,
|
|
||||||
serverDeliveredTimestamp,
|
|
||||||
uuid,
|
|
||||||
destinationUuid == null ? UuidUtil.UNKNOWN_UUID.toString() : destinationUuid,
|
|
||||||
isUrgent,
|
|
||||||
isStory,
|
|
||||||
null,
|
|
||||||
updatedPni == null ? "" : updatedPni);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,26 +113,8 @@ public class MessageCacheUtils {
|
||||||
try (var f = new FileOutputStream(file)) {
|
try (var f = new FileOutputStream(file)) {
|
||||||
try (var out = new DataOutputStream(f)) {
|
try (var out = new DataOutputStream(f)) {
|
||||||
out.writeInt(CURRENT_VERSION); // version
|
out.writeInt(CURRENT_VERSION); // version
|
||||||
out.writeInt(envelope.getType());
|
|
||||||
out.writeUTF(""); // legacy number
|
|
||||||
out.writeUTF(envelope.getSourceUuid().isPresent() ? envelope.getSourceUuid().get() : "");
|
|
||||||
out.writeInt(envelope.getSourceDevice());
|
|
||||||
out.writeUTF(envelope.getDestinationUuid() == null ? "" : envelope.getDestinationUuid());
|
|
||||||
out.writeLong(envelope.getTimestamp());
|
|
||||||
if (envelope.hasContent()) {
|
|
||||||
out.writeInt(envelope.getContent().length);
|
|
||||||
out.write(envelope.getContent());
|
|
||||||
} else {
|
|
||||||
out.writeInt(0);
|
|
||||||
}
|
|
||||||
out.writeInt(0); // legacy message
|
|
||||||
out.writeLong(envelope.getServerReceivedTimestamp());
|
|
||||||
var uuid = envelope.getServerGuid();
|
|
||||||
out.writeUTF(uuid == null ? "" : uuid);
|
|
||||||
out.writeLong(envelope.getServerDeliveredTimestamp());
|
out.writeLong(envelope.getServerDeliveredTimestamp());
|
||||||
out.writeBoolean(envelope.isUrgent());
|
envelope.getProto().writeTo(out);
|
||||||
out.writeBoolean(envelope.isStory());
|
|
||||||
out.writeUTF(envelope.getUpdatedPni() == null ? "" : envelope.getUpdatedPni());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue