Skip to content

Commit

Permalink
Better deserialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Oct 16, 2023
1 parent 5aba80e commit 0db352e
Show file tree
Hide file tree
Showing 27 changed files with 647 additions and 790 deletions.
25 changes: 16 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
<classgraph.version>4.8.160</classgraph.version>
<reflection.version>1.2</reflection.version>
<lazysodium.version>5.1.4</lazysodium.version>
<linkpreview.version>2.1</linkpreview.version>
<linkpreview.version>2.2</linkpreview.version>
<vcard.version>0.12.1</vcard.version>
<qr.terminal.version>2.2</qr.terminal.version>
<libphonenumber.version>8.13.13</libphonenumber.version>
Expand Down Expand Up @@ -232,7 +232,7 @@
<version>${qr.terminal.version}</version>
</dependency>

<!-- Cryptography -->
<!-- Buffer -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
Expand All @@ -243,6 +243,8 @@
<artifactId>bcpkix-jdk15on</artifactId>
<version>${bouncy.castle.version}</version>
</dependency>

<!-- Cryptography -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
Expand Down Expand Up @@ -277,6 +279,12 @@
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>


<!-- Scan for listeners - @RegisterListener -->
<dependency>
Expand All @@ -285,13 +293,6 @@
<version>${classgraph.version}</version>
</dependency>

<!-- Parse and generate vcards for ContactMessage -->
<dependency>
<groupId>com.googlecode.ez-vcard</groupId>
<artifactId>ez-vcard</artifactId>
<version>${vcard.version}</version>
</dependency>

<!-- Parse phone numbers (Mobile API) -->
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
Expand Down Expand Up @@ -331,6 +332,12 @@
<version>${poi.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.googlecode.ez-vcard</groupId>
<artifactId>ez-vcard</artifactId>
<version>${vcard.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/it/auties/whatsapp/api/ClientType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public enum ClientType {
* from <a href="https://web.whatsapp.com">Whatsapp Web Client</a>
*/
WEB,

/**
* A standalone client that requires an SMS code sent to the companion's phone number on log-in
* Reversed from <a href="https://github.com/tgalal/yowsup/issues/2910">KaiOS Mobile App</a>
Expand Down
181 changes: 29 additions & 152 deletions src/main/java/it/auties/whatsapp/api/ConnectionBuilder.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package it.auties.whatsapp.api;

import it.auties.whatsapp.controller.ControllerSerializer;
import it.auties.whatsapp.controller.DefaultControllerSerializer;
import it.auties.whatsapp.controller.Keys;
import it.auties.whatsapp.controller.Store;
import it.auties.whatsapp.model.companion.CompanionDevice;
import it.auties.whatsapp.model.mobile.PhoneNumber;
import it.auties.whatsapp.util.ControllerHelper;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -19,11 +17,10 @@
public final class ConnectionBuilder<T extends OptionsBuilder<T>> {
private final ClientType clientType;
private ControllerSerializer serializer;
private CompanionDevice device;

ConnectionBuilder(ClientType clientType) {
this.clientType = clientType;
this.serializer = DefaultControllerSerializer.instance();
this.serializer = ControllerSerializer.toSmile();
}

/**
Expand All @@ -37,17 +34,6 @@ public ConnectionBuilder<T> serializer(ControllerSerializer serializer) {
return this;
}

/**
* Sets the device to use
*
* @param device the non-null device to use
* @return the same instance for chaining
*/
public ConnectionBuilder<T> device(CompanionDevice device) {
this.device = device;
return this;
}

/**
* Creates a new connection using a random uuid
*
Expand All @@ -65,25 +51,11 @@ public T newConnection() {
* @param uuid the nullable uuid to use to create the connection
* @return a non-null options selector
*/
@SuppressWarnings("unchecked")
public T newConnection(UUID uuid) {
if (uuid == null) {
uuid = UUID.randomUUID();
}

var store = Store.builder()
.uuid(uuid)
.clientType(clientType)
.device(device)
.serializer(serializer);
var keys = Keys.builder()
.uuid(uuid)
.clientType(clientType)
.serializer(serializer);
return (T) switch (clientType) {
case WEB -> new WebOptionsBuilder(store, keys);
case MOBILE -> new MobileOptionsBuilder(store, keys);
};
var sessionUuid = Objects.requireNonNullElseGet(uuid, UUID::randomUUID);
var sessionStoreAndKeys = ControllerHelper.deserialize(sessionUuid, null, null, clientType, serializer)
.orElseGet(() -> ControllerHelper.create(sessionUuid, null, null, clientType, serializer));
return createConnection(sessionStoreAndKeys);
}

/**
Expand All @@ -94,24 +66,10 @@ public T newConnection(UUID uuid) {
* @param phoneNumber the nullable uuid to use to create the connection
* @return a non-null options selector
*/
@SuppressWarnings("unchecked")
public T newConnection(long phoneNumber) {
var uuid = UUID.randomUUID();
var store = Store.builder()
.uuid(uuid)
.clientType(clientType)
.device(device)
.phoneNumber(PhoneNumber.of(phoneNumber))
.serializer(serializer);
var keys = Keys.builder()
.uuid(uuid)
.clientType(clientType)
.phoneNumber(PhoneNumber.of(phoneNumber))
.serializer(serializer);
return (T) switch (clientType) {
case WEB -> new WebOptionsBuilder(store, keys);
case MOBILE -> new MobileOptionsBuilder(store, keys);
};
var sessionStoreAndKeys = ControllerHelper.deserialize(null, phoneNumber, null, clientType, serializer)
.orElseGet(() -> ControllerHelper.create(UUID.randomUUID(), phoneNumber, null, clientType, serializer));
return createConnection(sessionStoreAndKeys);
}

/**
Expand All @@ -122,24 +80,10 @@ public T newConnection(long phoneNumber) {
* @param alias the nullable alias to use to create the connection
* @return a non-null options selector
*/
@SuppressWarnings("unchecked")
public T newConnection(String alias) {
var uuid = UUID.randomUUID();
var store = Store.builder()
.uuid(uuid)
.clientType(clientType)
.device(device)
.serializer(serializer)
.alias(alias);
var keys = Keys.builder()
.uuid(uuid)
.clientType(clientType)
.serializer(serializer)
.alias(alias);
return (T) switch (clientType) {
case WEB -> new WebOptionsBuilder(store, keys);
case MOBILE -> new MobileOptionsBuilder(store, keys);
};
var sessionStoreAndKeys = ControllerHelper.deserialize(null, null, alias, clientType, serializer)
.orElseGet(() -> ControllerHelper.create(UUID.randomUUID(), null, alias != null ? List.of(alias) : null, clientType, serializer));
return createConnection(sessionStoreAndKeys);
}

/**
Expand Down Expand Up @@ -168,35 +112,10 @@ public T lastConnection() {
*
* @return a non-null options selector
*/
@SuppressWarnings({"unchecked", "OptionalIsPresent"})
public Optional<T> newOptionalConnection(UUID uuid) {
if (uuid == null) {
uuid = UUID.randomUUID();
}

var store = Store.builder()
.uuid(uuid)
.clientType(clientType)
.device(device)
.serializer(serializer)
.deserialize();
if (store.isEmpty()) {
return Optional.empty();
}

var keys = Keys.builder()
.uuid(uuid)
.clientType(clientType)
.serializer(serializer)
.deserialize();
if (keys.isEmpty()) {
return Optional.empty();
}

return Optional.of((T) switch (clientType) {
case WEB -> new WebOptionsBuilder(store.get(), keys.get());
case MOBILE -> new MobileOptionsBuilder(store.get(), keys.get());
});
var sessionUuid = Objects.requireNonNullElseGet(uuid, UUID::randomUUID);
var sessionStoreAndKeys = ControllerHelper.deserialize(sessionUuid, null, null, clientType, serializer);
return sessionStoreAndKeys.map(this::createConnection);
}

/**
Expand All @@ -205,34 +124,9 @@ public Optional<T> newOptionalConnection(UUID uuid) {
*
* @return a non-null options selector
*/
@SuppressWarnings({"unchecked", "OptionalIsPresent"})
public Optional<T> newOptionalConnection(Long phoneNumber) {
var uuid = UUID.randomUUID();
var store = Store.builder()
.uuid(uuid)
.clientType(clientType)
.device(device)
.phoneNumber(PhoneNumber.ofNullable(phoneNumber).orElse(null))
.serializer(serializer)
.deserialize();
if (store.isEmpty()) {
return Optional.empty();
}

var keys = Keys.builder()
.uuid(uuid)
.clientType(clientType)
.phoneNumber(PhoneNumber.ofNullable(phoneNumber).orElse(null))
.serializer(serializer)
.deserialize();
if (keys.isEmpty()) {
return Optional.empty();
}

return Optional.of((T) switch (clientType) {
case WEB -> new WebOptionsBuilder(store.get(), keys.get());
case MOBILE -> new MobileOptionsBuilder(store.get(), keys.get());
});
var sessionStoreAndKeys = ControllerHelper.deserialize(null, phoneNumber, null, clientType, serializer);
return sessionStoreAndKeys.map(this::createConnection);
}

/**
Expand All @@ -242,34 +136,9 @@ public Optional<T> newOptionalConnection(Long phoneNumber) {
* @param alias the nullable alias to use to create the connection
* @return a non-null options selector
*/
@SuppressWarnings({"unchecked", "OptionalIsPresent"})
public Optional<T> newOptionalConnection(String alias) {
var uuid = UUID.randomUUID();
var store = Store.builder()
.uuid(uuid)
.clientType(clientType)
.device(device)
.serializer(serializer)
.alias(alias)
.deserialize();
if (store.isEmpty()) {
return Optional.empty();
}

var keys = Keys.builder()
.uuid(uuid)
.clientType(clientType)
.serializer(serializer)
.alias(alias)
.deserialize();
if (keys.isEmpty()) {
return Optional.empty();
}

return Optional.of((T) switch (clientType) {
case WEB -> new WebOptionsBuilder(store.get(), keys.get());
case MOBILE -> new MobileOptionsBuilder(store.get(), keys.get());
});
var sessionStoreAndKeys = ControllerHelper.deserialize(null, null, alias, clientType, serializer);
return sessionStoreAndKeys.map(this::createConnection);
}

/**
Expand All @@ -289,4 +158,12 @@ public Optional<T> firstOptionalConnection() {
public Optional<T> lastOptionalConnection() {
return newOptionalConnection(serializer.listIds(clientType).peekLast());
}

@SuppressWarnings("unchecked")
private T createConnection(ControllerHelper.StoreAndKeysPair sessionStoreAndKeys) {
return (T) switch (clientType) {
case WEB -> new WebOptionsBuilder(sessionStoreAndKeys.store(), sessionStoreAndKeys.keys());
case MOBILE -> new MobileOptionsBuilder(sessionStoreAndKeys.store(), sessionStoreAndKeys.keys());
};
}
}
Loading

0 comments on commit 0db352e

Please sign in to comment.