Skip to content

Commit

Permalink
Working on protobuf serialization instead of relying on smile
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Jan 7, 2024
1 parent 43a6af8 commit dd7faeb
Show file tree
Hide file tree
Showing 109 changed files with 1,152 additions and 582 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@

<dependencies>
<!-- Generate and print QR code (Web API) -->
<dependency>
<groupId>com.github.auties00</groupId>
<artifactId>protobuf-serialization-plugin</artifactId>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
Expand Down Expand Up @@ -279,10 +284,10 @@
<version>${protoc.version}</version>
</dependency>

<!-- Smile serialization (used to persist sessions) -->
<!-- Json serialization (used by Whatsapp) -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-smile</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion proto/whatsapp.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
syntax = "proto2";
syntax = "proto3";
package unsupported;

message ADVDeviceIdentity {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/it/auties/whatsapp/api/ClientType.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package it.auties.whatsapp.api;

import it.auties.protobuf.annotation.ProtobufEnumIndex;
import it.auties.protobuf.model.ProtobufEnum;

/**
* The constants of this enumerated type describe the various types of API that can be used to make
* {@link Whatsapp} work
*/
public enum ClientType {
public enum ClientType implements ProtobufEnum {
/**
* A standalone client that requires the QR code to be scanned by its companion on log-in Reversed
* from <a href="https://web.whatsapp.com">Whatsapp Web Client</a>
*/
WEB,
WEB(0),
/**
* 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>
*/
MOBILE
MOBILE(1);

final int index;
ClientType(@ProtobufEnumIndex int index) {
this.index = index;
}
}
21 changes: 17 additions & 4 deletions src/main/java/it/auties/whatsapp/api/TextPreviewSetting.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package it.auties.whatsapp.api;

import it.auties.protobuf.annotation.ProtobufEnumIndex;
import it.auties.protobuf.model.ProtobufEnum;

/**
* The constants of this enumerated type describe the various types of text preview that can be
* used
*/
public enum TextPreviewSetting {
public enum TextPreviewSetting implements ProtobufEnum {
/**
* Link previews will be generated. If a message contains an url without a schema(for example
* wikipedia.com), the message will be autocorrected to include it and a preview will be
* generated
*/
ENABLED_WITH_INFERENCE,
ENABLED_WITH_INFERENCE(0),

/**
* Link previews will be generated. No inference will be used.
*/
ENABLED,
ENABLED(1),

/**
* Link previews will not be generated
*/
DISABLED
DISABLED(2);

final int index;

TextPreviewSetting(@ProtobufEnumIndex int index) {
this.index = index;
}

public int index() {
return index;
}
}
8 changes: 7 additions & 1 deletion src/main/java/it/auties/whatsapp/api/WebHistoryLength.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package it.auties.whatsapp.api;

import it.auties.protobuf.annotation.ProtobufProperty;
import it.auties.protobuf.model.ProtobufMessage;
import it.auties.protobuf.model.ProtobufType;
import it.auties.whatsapp.util.Specification;

/**
* The constants of this enumerated type describe the various chat history's codeLength that Whatsapp
* can send on the first login attempt
*/
public record WebHistoryLength(int size) {
public record WebHistoryLength(
@ProtobufProperty(index = 1, type = ProtobufType.INT32)
int size
) implements ProtobufMessage {
private static final WebHistoryLength ZERO = new WebHistoryLength(0);
private static final WebHistoryLength STANDARD = new WebHistoryLength(Specification.Whatsapp.DEFAULT_HISTORY_SIZE);
private static final WebHistoryLength EXTENDED = new WebHistoryLength(Integer.MAX_VALUE);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/it/auties/whatsapp/api/Whatsapp.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ public CompletableFuture<NewsletterMessageInfo> sendNewsletterMessage(JidProvide
.map(NewsletterMessageInfo::serverId)
.orElse(0);
var info = new NewsletterMessageInfo(
newsletter.get(),
ChatMessageKey.randomId(),
oldServerId + 1,
Clock.nowSeconds(),
Expand All @@ -485,6 +484,7 @@ public CompletableFuture<NewsletterMessageInfo> sendNewsletterMessage(JidProvide
message,
MessageStatus.PENDING
);
info.setNewsletter(newsletter.get());
return sendMessage(info);
}

Expand All @@ -504,7 +504,6 @@ public <T extends MessageInfo> CompletableFuture<T> editMessage(T oldMessage, Me
return switch (oldMessage) {
case NewsletterMessageInfo oldNewsletterInfo -> {
var info = new NewsletterMessageInfo(
oldNewsletterInfo.newsletter(),
oldNewsletterInfo.id(),
oldNewsletterInfo.serverId(),
Clock.nowSeconds(),
Expand All @@ -513,6 +512,7 @@ public <T extends MessageInfo> CompletableFuture<T> editMessage(T oldMessage, Me
MessageContainer.ofEditedMessage(newMessage),
MessageStatus.PENDING
);
info.setNewsletter(oldNewsletterInfo.newsletter());
var request = new MessageSendRequest.Newsletter(info, Map.of("edit", getEditBit(info)));
yield socketHandler.sendMessage(request)
.thenApply(ignored -> oldMessage);
Expand Down Expand Up @@ -1528,7 +1528,6 @@ public CompletableFuture<Void> unarchive(JidProvider chat) {
*/
public CompletableFuture<Void> deleteMessage(NewsletterMessageInfo messageInfo) {
var revokeInfo = new NewsletterMessageInfo(
messageInfo.newsletter(),
messageInfo.id(),
messageInfo.serverId(),
Clock.nowSeconds(),
Expand All @@ -1537,6 +1536,7 @@ public CompletableFuture<Void> deleteMessage(NewsletterMessageInfo messageInfo)
MessageContainer.empty(),
MessageStatus.PENDING
);
revokeInfo.setNewsletter(messageInfo.newsletter());
var attrs = Map.of("edit", getDeleteBit(messageInfo));
var request = new MessageSendRequest.Newsletter(revokeInfo, attrs);
return socketHandler.sendMessage(request);
Expand Down Expand Up @@ -2509,7 +2509,7 @@ private CompletableFuture<Call> sendCallMessage(JidProvider provider) {
}

private Call onCallSent(JidProvider provider, String callId, Node result) {
var call = new Call(provider.toJid(), jidOrThrowError(), callId, ZonedDateTime.now(), false, CallStatus.RINGING, false);
var call = new Call(provider.toJid(), jidOrThrowError(), callId, Clock.nowSeconds(), false, CallStatus.RINGING, false);
store().addCall(call);
socketHandler.onCall(call);
return call;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/it/auties/whatsapp/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package it.auties.whatsapp.controller;

import com.fasterxml.jackson.annotation.JsonIgnore;
import it.auties.protobuf.annotation.ProtobufProperty;
import it.auties.protobuf.model.ProtobufMessage;
import it.auties.protobuf.model.ProtobufType;
import it.auties.whatsapp.api.ClientType;
import it.auties.whatsapp.model.mobile.PhoneNumber;
import it.auties.whatsapp.util.Json;
import it.auties.whatsapp.util.ProtobufUuidMixin;

import java.util.*;

Expand All @@ -12,15 +16,17 @@
* way to store IDs and serialize said class.
*/
@SuppressWarnings("unused")
public abstract sealed class Controller<T extends Controller<T>> permits Store, Keys {
public abstract sealed class Controller<T extends Controller<T>> implements ProtobufMessage permits Store, Keys {
/**
* The id of this controller
*/
@ProtobufProperty(index = 1, type = ProtobufType.STRING, mixin = ProtobufUuidMixin.class)
protected final UUID uuid;

/**
* The phone number of the associated companion
*/
@ProtobufProperty(index = 2, type = ProtobufType.UINT64)
private PhoneNumber phoneNumber;

/**
Expand All @@ -32,11 +38,13 @@ public abstract sealed class Controller<T extends Controller<T>> permits Store,
/**
* The client type
*/
@ProtobufProperty(index = 3, type = ProtobufType.OBJECT)
protected final ClientType clientType;

/**
* A list of alias for the controller, can be used in place of UUID1
*/
@ProtobufProperty(index = 4, type = ProtobufType.STRING)
protected final Collection<String> alias;

public Controller(UUID uuid, PhoneNumber phoneNumber, ControllerSerializer serializer, ClientType clientType, Collection<String> alias) {
Expand All @@ -60,7 +68,7 @@ public Controller(UUID uuid, PhoneNumber phoneNumber, ControllerSerializer seria
public abstract void dispose();

public UUID uuid() {
return this.uuid;
return uuid;
}

public ClientType clientType() {
Expand Down
Loading

0 comments on commit dd7faeb

Please sign in to comment.