Java Code Examples for org.whispersystems.signalservice.api.util.UuidUtil#parseOrNull()

The following examples show how to use org.whispersystems.signalservice.api.util.UuidUtil#parseOrNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceDataMessage.Quote createQuote(SignalServiceProtos.DataMessage content) throws ProtocolInvalidMessageException {
  if (!content.hasQuote()) return null;

  List<SignalServiceDataMessage.Quote.QuotedAttachment> attachments = new LinkedList<>();

  for (SignalServiceProtos.DataMessage.Quote.QuotedAttachment attachment : content.getQuote().getAttachmentsList()) {
    attachments.add(new SignalServiceDataMessage.Quote.QuotedAttachment(attachment.getContentType(),
                                                                        attachment.getFileName(),
                                                                        attachment.hasThumbnail() ? createAttachmentPointer(attachment.getThumbnail()) : null));
  }

  if (SignalServiceAddress.isValidAddress(content.getQuote().getAuthorUuid(), content.getQuote().getAuthorE164())) {
    SignalServiceAddress address = new SignalServiceAddress(UuidUtil.parseOrNull(content.getQuote().getAuthorUuid()), content.getQuote().getAuthorE164());

    return new SignalServiceDataMessage.Quote(content.getQuote().getId(),
                                              address,
                                              content.getQuote().getText(),
                                              attachments);
  } else {
    Log.w(TAG, "Quote was missing an author! Returning null.");
    return null;
  }
}
 
Example 2
Source File: SignalServiceContent.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static SignalServiceDataMessage.Reaction createReaction(SignalServiceProtos.DataMessage content) {
  if (!content.hasReaction()                                                                        ||
      !content.getReaction().hasEmoji()                                                             ||
      !(content.getReaction().hasTargetAuthorE164() || content.getReaction().hasTargetAuthorUuid()) ||
      !content.getReaction().hasTargetSentTimestamp())
  {
    return null;
  }

  SignalServiceProtos.DataMessage.Reaction reaction = content.getReaction();

  return new SignalServiceDataMessage.Reaction(reaction.getEmoji(),
                      reaction.getRemove(),
                      new SignalServiceAddress(UuidUtil.parseOrNull(reaction.getTargetAuthorUuid()), reaction.getTargetAuthorE164()),
                      reaction.getTargetSentTimestamp());
}
 
Example 3
Source File: SignalServiceCipher.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
private SignalServiceDataMessage.Quote createQuote(DataMessage content) {
  if (!content.hasQuote()) return null;

  List<SignalServiceDataMessage.Quote.QuotedAttachment> attachments = new LinkedList<>();

  for (DataMessage.Quote.QuotedAttachment attachment : content.getQuote().getAttachmentsList()) {
    attachments.add(new SignalServiceDataMessage.Quote.QuotedAttachment(attachment.getContentType(),
                                                                        attachment.getFileName(),
                                                                        attachment.hasThumbnail() ? createAttachmentPointer(attachment.getThumbnail()) : null));
  }

  if (SignalServiceAddress.isValidAddress(content.getQuote().getAuthorUuid(), content.getQuote().getAuthorE164())) {
    SignalServiceAddress address = new SignalServiceAddress(UuidUtil.parseOrNull(content.getQuote().getAuthorUuid()), content.getQuote().getAuthorE164());

    return new SignalServiceDataMessage.Quote(content.getQuote().getId(),
                                              address,
                                              content.getQuote().getText(),
                                              attachments);
  } else {
    Log.w(TAG, "Quote was missing an author! Returning null.");
    return null;
  }
}
 
Example 4
Source File: Manager.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
public void verifyAccount(String verificationCode, String pin) throws IOException {
    verificationCode = verificationCode.replace("-", "");
    account.setSignalingKey(KeyUtils.createSignalingKey());
    // TODO make unrestricted unidentified access configurable
    VerifyAccountResponse response = accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, null, getSelfUnidentifiedAccessKey(), false, ServiceConfig.capabilities);

    UUID uuid = UuidUtil.parseOrNull(response.getUuid());
    // TODO response.isStorageCapable()
    //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
    account.setRegistered(true);
    account.setUuid(uuid);
    account.setRegistrationLockPin(pin);
    account.getSignalProtocolStore().saveIdentity(account.getSelfAddress(), getIdentityKeyPair().getPublicKey(), TrustLevel.TRUSTED_VERIFIED);

    refreshPreKeys();
    account.save();
}
 
Example 5
Source File: SignalContactRecord.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public SignalContactRecord(StorageId id, ContactRecord proto) {
  this.id    = id;
  this.proto = proto;

  this.address     = new SignalServiceAddress(UuidUtil.parseOrNull(proto.getServiceUuid()), proto.getServiceE164());
  this.givenName   = OptionalUtil.absentIfEmpty(proto.getGivenName());
  this.familyName  = OptionalUtil.absentIfEmpty(proto.getFamilyName());
  this.profileKey  = OptionalUtil.absentIfEmpty(proto.getProfileKey());
  this.username    = OptionalUtil.absentIfEmpty(proto.getUsername());
  this.identityKey = OptionalUtil.absentIfEmpty(proto.getIdentityKey());
}
 
Example 6
Source File: Util.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
public static SignalServiceAddress getSignalServiceAddressFromIdentifier(final String identifier) {
    if (UuidUtil.isUuid(identifier)) {
        return new SignalServiceAddress(UuidUtil.parseOrNull(identifier), null);
    } else {
        return new SignalServiceAddress(null, identifier);
    }
}
 
Example 7
Source File: JsonSessionStore.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
@Override
public JsonSessionStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode node = jsonParser.getCodec().readTree(jsonParser);

    JsonSessionStore sessionStore = new JsonSessionStore();

    if (node.isArray()) {
        for (JsonNode session : node) {
            String sessionName = session.has("name")
                    ? session.get("name").asText()
                    : null;
            if (UuidUtil.isUuid(sessionName)) {
                // Ignore sessions that were incorrectly created with UUIDs as name
                continue;
            }

            UUID uuid = session.hasNonNull("uuid")
                    ? UuidUtil.parseOrNull(session.get("uuid").asText())
                    : null;
            final SignalServiceAddress serviceAddress = uuid == null
                    ? Util.getSignalServiceAddressFromIdentifier(sessionName)
                    : new SignalServiceAddress(uuid, sessionName);
            final int deviceId = session.get("deviceId").asInt();
            final String record = session.get("record").asText();
            try {
                SessionInfo sessionInfo = new SessionInfo(serviceAddress, deviceId, Base64.decode(record));
                sessionStore.sessions.add(sessionInfo);
            } catch (IOException e) {
                System.out.println(String.format("Error while decoding session for: %s", sessionName));
            }
        }
    }

    return sessionStore;
}
 
Example 8
Source File: SignalServiceAddress.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isValidAddress(String rawUuid, String e164) {
  return (e164 != null && !e164.isEmpty()) || UuidUtil.parseOrNull(rawUuid) != null;
}
 
Example 9
Source File: SignalServiceEnvelope.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return The envelope's sender as a SignalServiceAddress.
 */
public SignalServiceAddress getSourceAddress() {
  return new SignalServiceAddress(UuidUtil.parseOrNull(envelope.getSourceUuid()), envelope.getSourceE164());
}
 
Example 10
Source File: JsonUtil.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UUID deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
  return UuidUtil.parseOrNull(p.getValueAsString());
}
 
Example 11
Source File: TextSecurePreferences.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public static UUID getLocalUuid(Context context) {
  return UuidUtil.parseOrNull(getStringPreference(context, LOCAL_UUID_PREF, null));
}
 
Example 12
Source File: SignalServiceAddress.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isValidAddress(String rawUuid, String e164) {
  return (e164 != null && !e164.isEmpty()) || UuidUtil.parseOrNull(rawUuid) != null;
}
 
Example 13
Source File: DeviceContactsInputStream.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public DeviceContact read() throws IOException {
  long   detailsLength     = readRawVarint32();
  byte[] detailsSerialized = new byte[(int)detailsLength];
  Util.readFully(in, detailsSerialized);

  SignalServiceProtos.ContactDetails details = SignalServiceProtos.ContactDetails.parseFrom(detailsSerialized);

  if (!SignalServiceAddress.isValidAddress(details.getUuid(), details.getNumber())) {
    throw new IOException("Missing contact address!");
  }

  SignalServiceAddress                    address     = new SignalServiceAddress(UuidUtil.parseOrNull(details.getUuid()), details.getNumber());
  Optional<String>                        name        = Optional.fromNullable(details.getName());
  Optional<SignalServiceAttachmentStream> avatar      = Optional.absent();
  Optional<String>                        color       = details.hasColor() ? Optional.of(details.getColor()) : Optional.<String>absent();
  Optional<VerifiedMessage>               verified    = Optional.absent();
  Optional<byte[]>                        profileKey  = Optional.absent();
  boolean                                 blocked     = false;
  Optional<Integer>                       expireTimer = Optional.absent();

  if (details.hasAvatar()) {
    long        avatarLength      = details.getAvatar().getLength();
    InputStream avatarStream      = new LimitedInputStream(in, avatarLength);
    String      avatarContentType = details.getAvatar().getContentType();

    avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, null));
  }

  if (details.hasVerified()) {
    try {
      if (!SignalServiceAddress.isValidAddress(details.getVerified().getDestinationUuid(), details.getVerified().getDestinationE164())) {
        throw new InvalidMessageException("Missing Verified address!");
      }
      IdentityKey          identityKey = new IdentityKey(details.getVerified().getIdentityKey().toByteArray(), 0);
      SignalServiceAddress destination = new SignalServiceAddress(UuidUtil.parseOrNull(details.getVerified().getDestinationUuid()),
                                                                  details.getVerified().getDestinationE164());

      VerifiedMessage.VerifiedState state;

      switch (details.getVerified().getState()) {
        case VERIFIED:  state = VerifiedMessage.VerifiedState.VERIFIED;   break;
        case UNVERIFIED:state = VerifiedMessage.VerifiedState.UNVERIFIED; break;
        case DEFAULT:   state = VerifiedMessage.VerifiedState.DEFAULT;    break;
        default:        throw new InvalidMessageException("Unknown state: " + details.getVerified().getState());
      }

      verified = Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
    } catch (InvalidKeyException | InvalidMessageException e) {
      Log.w(TAG, e);
      verified = Optional.absent();
    }
  }

  if (details.hasProfileKey()) {
    profileKey = Optional.fromNullable(details.getProfileKey().toByteArray());
  }

  if (details.hasExpireTimer() && details.getExpireTimer() > 0) {
    expireTimer = Optional.of(details.getExpireTimer());
  }

  blocked = details.getBlocked();

  return new DeviceContact(address, name, avatar, color, verified, profileKey, blocked, expireTimer);
}
 
Example 14
Source File: SignalServiceEnvelope.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return The envelope's sender as a SignalServiceAddress.
 */
public SignalServiceAddress getSourceAddress() {
  return new SignalServiceAddress(UuidUtil.parseOrNull(envelope.getSourceUuid()), envelope.getSourceE164());
}
 
Example 15
Source File: Utils.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
static SignalServiceEnvelope loadEnvelope(File file) throws IOException {
    try (FileInputStream f = new FileInputStream(file)) {
        DataInputStream in = new DataInputStream(f);
        int version = in.readInt();
        if (version > 3) {
            return null;
        }
        int type = in.readInt();
        String source = in.readUTF();
        UUID sourceUuid = null;
        if (version >= 3) {
            sourceUuid = UuidUtil.parseOrNull(in.readUTF());
        }
        int sourceDevice = in.readInt();
        if (version == 1) {
            // read legacy relay field
            in.readUTF();
        }
        long timestamp = in.readLong();
        byte[] content = null;
        int contentLen = in.readInt();
        if (contentLen > 0) {
            content = new byte[contentLen];
            in.readFully(content);
        }
        byte[] legacyMessage = null;
        int legacyMessageLen = in.readInt();
        if (legacyMessageLen > 0) {
            legacyMessage = new byte[legacyMessageLen];
            in.readFully(legacyMessage);
        }
        long serverTimestamp = 0;
        String uuid = null;
        if (version == 2) {
            serverTimestamp = in.readLong();
            uuid = in.readUTF();
            if ("".equals(uuid)) {
                uuid = null;
            }
        }
        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);
    }
}