]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/manager/Utils.java
Support saving profiles for users without uuids
[signal-cli] / src / main / java / org / asamk / signal / manager / Utils.java
index 0b74f01d90b0d9060fd548224f7fcabd1638cf88..05fcfb5ed84f7069f592d7e3c08b321b1e3d1a10 100644 (file)
@@ -1,6 +1,5 @@
 package org.asamk.signal.manager;
 
-import org.asamk.signal.AttachmentInvalidException;
 import org.signal.libsignal.metadata.certificate.CertificateValidator;
 import org.whispersystems.libsignal.IdentityKey;
 import org.whispersystems.libsignal.InvalidKeyException;
@@ -15,6 +14,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.api.util.StreamDetails;
 import org.whispersystems.signalservice.api.util.UuidUtil;
+import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
 import org.whispersystems.util.Base64;
 
 import java.io.BufferedInputStream;
@@ -58,7 +58,7 @@ class Utils {
         return signalServiceAttachments;
     }
 
-    private static String getFileMimeType(File file) throws IOException {
+    static String getFileMimeType(File file, String defaultMimeType) throws IOException {
         String mime = Files.probeContentType(file.toPath());
         if (mime == null) {
             try (InputStream bufferedStream = new BufferedInputStream(new FileInputStream(file))) {
@@ -66,7 +66,7 @@ class Utils {
             }
         }
         if (mime == null) {
-            mime = "application/octet-stream";
+            return defaultMimeType;
         }
         return mime;
     }
@@ -74,12 +74,14 @@ class Utils {
     static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
         InputStream attachmentStream = new FileInputStream(attachmentFile);
         final long attachmentSize = attachmentFile.length();
-        final String mime = getFileMimeType(attachmentFile);
-        // TODO mabybe add a parameter to set the voiceNote, preview, width, height and caption option
+        final String mime = getFileMimeType(attachmentFile, "application/octet-stream");
+        // TODO mabybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
+        final long uploadTimestamp = System.currentTimeMillis();
         Optional<byte[]> preview = Optional.absent();
         Optional<String> caption = Optional.absent();
         Optional<String> blurHash = Optional.absent();
-        return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, preview, 0, 0, caption, blurHash, null, null);
+        final Optional<ResumableUploadSpec> resumableUploadSpec = Optional.absent();
+        return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, false, preview, 0, 0, uploadTimestamp, caption, blurHash, null, null, resumableUploadSpec);
     }
 
     static StreamDetails createStreamDetailsFromFile(File file) throws IOException {
@@ -94,7 +96,7 @@ class Utils {
 
     static CertificateValidator getCertificateValidator() {
         try {
-            ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(BaseConfig.UNIDENTIFIED_SENDER_TRUST_ROOT), 0);
+            ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(ServiceConfig.UNIDENTIFIED_SENDER_TRUST_ROOT), 0);
             return new CertificateValidator(unidentifiedSenderTrustRoot);
         } catch (InvalidKeyException | IOException e) {
             throw new AssertionError(e);
@@ -150,7 +152,7 @@ class Utils {
         try (FileInputStream f = new FileInputStream(file)) {
             DataInputStream in = new DataInputStream(f);
             int version = in.readInt();
-            if (version > 3) {
+            if (version > 4) {
                 return null;
             }
             int type = in.readInt();
@@ -177,26 +179,30 @@ class Utils {
                 legacyMessage = new byte[legacyMessageLen];
                 in.readFully(legacyMessage);
             }
-            long serverTimestamp = 0;
+            long serverReceivedTimestamp = 0;
             String uuid = null;
-            if (version == 2) {
-                serverTimestamp = in.readLong();
+            if (version >= 2) {
+                serverReceivedTimestamp = in.readLong();
                 uuid = in.readUTF();
                 if ("".equals(uuid)) {
                     uuid = null;
                 }
             }
+            long serverDeliveredTimestamp = 0;
+            if (version >= 4) {
+                serverDeliveredTimestamp = in.readLong();
+            }
             Optional<SignalServiceAddress> addressOptional = sourceUuid == null && source.isEmpty()
                     ? Optional.absent()
                     : Optional.of(new SignalServiceAddress(sourceUuid, source));
-            return new SignalServiceEnvelope(type, addressOptional, sourceDevice, timestamp, legacyMessage, content, serverTimestamp, uuid);
+            return new SignalServiceEnvelope(type, addressOptional, sourceDevice, timestamp, legacyMessage, content, serverReceivedTimestamp, serverDeliveredTimestamp, uuid);
         }
     }
 
     static void storeEnvelope(SignalServiceEnvelope envelope, File file) throws IOException {
         try (FileOutputStream f = new FileOutputStream(file)) {
             try (DataOutputStream out = new DataOutputStream(f)) {
-                out.writeInt(3); // version
+                out.writeInt(4); // version
                 out.writeInt(envelope.getType());
                 out.writeUTF(envelope.getSourceE164().isPresent() ? envelope.getSourceE164().get() : "");
                 out.writeUTF(envelope.getSourceUuid().isPresent() ? envelope.getSourceUuid().get() : "");
@@ -214,9 +220,10 @@ class Utils {
                 } else {
                     out.writeInt(0);
                 }
-                out.writeLong(envelope.getServerTimestamp());
+                out.writeLong(envelope.getServerReceivedTimestamp());
                 String uuid = envelope.getUuid();
                 out.writeUTF(uuid == null ? "" : uuid);
+                out.writeLong(envelope.getServerDeliveredTimestamp());
             }
         }
     }
@@ -243,7 +250,7 @@ class Utils {
         byte[] ownId;
         byte[] theirId;
 
-        if (BaseConfig.capabilities.isUuid()
+        if (ServiceConfig.capabilities.isUuid()
                 && ownAddress.getUuid().isPresent() && theirAddress.getUuid().isPresent()) {
             // Version 2: UUID user
             version = 2;
@@ -252,6 +259,9 @@ class Utils {
         } else {
             // Version 1: E164 user
             version = 1;
+            if (!ownAddress.getNumber().isPresent() || !theirAddress.getNumber().isPresent()) {
+                return "INVALID ID";
+            }
             ownId = ownAddress.getNumber().get().getBytes();
             theirId = theirAddress.getNumber().get().getBytes();
         }