Skip to content

Commit

Permalink
Release 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Aug 24, 2024
1 parent a4c04ff commit 58cf2aa
Show file tree
Hide file tree
Showing 435 changed files with 6,892 additions and 4,677 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ In short, if you use this library without a malicious intent, you will never get
<dependency>
<groupId>com.github.auties00</groupId>
<artifactId>cobalt</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
</dependency>
```

Expand All @@ -67,7 +67,7 @@ In short, if you use this library without a malicious intent, you will never get
<annotationProcessorPath>
<groupId>com.github.auties00</groupId>
<artifactId>cobalt</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<annotationProcessorPath>
<annotationProcessorPaths>
<configuration>
Expand All @@ -79,23 +79,23 @@ In short, if you use this library without a malicious intent, you will never get
- Groovy DSL
- Dependency
```groovy
implementation 'com.github.auties00:cobalt:0.0.5'
implementation 'com.github.auties00:cobalt:0.0.6'
```

- Annotation processor (required for @RegisterListener)
```groovy
annotationProcessor 'com.github.auties00:cobalt:0.0.5'
annotationProcessor 'com.github.auties00:cobalt:0.0.6'
```

- Kotlin DSL
- Dependency
```groovy
implementation("com.github.auties00:cobalt:0.0.5")
implementation("com.github.auties00:cobalt:0.0.6")
```

- Annotation processor (required for @RegisterListener)
```groovy
annotationProcessor("com.github.auties00:cobalt:0.0.5")
annotationProcessor("com.github.auties00:cobalt:0.0.6")
```

### Javadocs & Documentation
Expand Down Expand Up @@ -285,6 +285,10 @@ Now you can connect to your session:
```
to connect to Whatsapp.
Remember to handle the result using, for example, `join` to await the connection's result.
Finally, if you want to pause the current thread until the connection is closed, use:
```java
.awaitDisconnection()
```
</details>

<details>
Expand All @@ -298,7 +302,8 @@ Remember to handle the result using, for example, `join` to await the connection
.addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason)) // Print a message when disconnected
.addNewChatMessageListener(message -> System.out.printf("New message: %s%n", message.toJson())) // Print a message when a new chat message arrives
.connect() // Connect to Whatsapp asynchronously
.join(); // Await the result
.join() // Await the result
.awaitDisconnection(); // Wait
```
</details>

Expand All @@ -316,7 +321,8 @@ Remember to handle the result using, for example, `join` to await the connection
.addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason)) // Print a message when disconnected
.addNewChatMessageListener(message -> System.out.printf("New message: %s%n", message.toJson())) // Print a message when a new chat message arrives
.connect() // Connect to Whatsapp asynchronously
.join(); // Await the result
.join() // Await the result
.awaitDisconnection(); // Wait
```
</details>

Expand All @@ -343,7 +349,8 @@ Remember to handle the result using, for example, `join` to await the connection
.addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason)) // Print a message when disconnected
.addNewChatMessageListener(message -> System.out.printf("New message: %s%n", message.toJson())) // Print a message when a new chat message arrives
.connect() // Connect to Whatsapp asynchronously
.join(); // Await the result
.join() // Await the result
.awaitDisconnection(); // Wait
```
</details>

Expand Down
35 changes: 31 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.auties00</groupId>
<artifactId>cobalt</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Standalone fully-featured Whatsapp Web API for Java and Kotlin</description>
<url>https://github.com/Auties00/Cobalt</url>
Expand Down Expand Up @@ -178,7 +178,7 @@
<maven.javadoc.plugin.version>3.5.0</maven.javadoc.plugin.version>
<maven.nexus.plugin.version>1.6.13</maven.nexus.plugin.version>
<zxing.version>3.5.1</zxing.version>
<protoc.version>3.0.4</protoc.version>
<protoc.version>3.2.1</protoc.version>
<junit.version>5.10.0-M1</junit.version>
<jackson.version>2.15.2</jackson.version>
<curve25519.version>1.2</curve25519.version>
Expand All @@ -191,6 +191,9 @@
<plist.version>1.27</plist.version>
<aspose.words.version>22.11</aspose.words.version>
<ffmpeg.version>2023.09.10</ffmpeg.version>
<http.version>5.3</http.version>
<sl4j.version>2.0.13</sl4j.version>
<apk.parser.version>2.6.10</apk.parser.version>
</properties>

<build>
Expand All @@ -216,7 +219,7 @@
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>it.auties.whatsapp.listener.processor.RegisterListenerProcessor</annotationProcessor>
<annotationProcessor>it.auties.whatsapp.listener.RegisterListenerProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
Expand Down Expand Up @@ -281,6 +284,11 @@
<artifactId>protobuf-base</artifactId>
<version>${protoc.version}</version>
</dependency>
<dependency>
<groupId>com.github.auties00</groupId>
<artifactId>protobuf-serialization-plugin</artifactId>
<version>${protoc.version}</version>
</dependency>

<!-- Json serialization (used by Whatsapp) -->
<dependency>
Expand Down Expand Up @@ -318,6 +326,13 @@
<version>${plist.version}</version>
</dependency>

<!-- Parse Android APK for token generation (Mobile API) -->
<dependency>
<groupId>net.dongliu</groupId>
<artifactId>apk-parser</artifactId>
<version>${apk.parser.version}</version>
</dependency>

<!-- Generate message previews -->
<dependency>
<groupId>com.github.auties00</groupId>
Expand All @@ -341,6 +356,18 @@
<version>${vcard.version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
</dependency>

<!-- Testing framework -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/it/auties/whatsapp/api/ClientType.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package it.auties.whatsapp.api;

import it.auties.protobuf.annotation.ProtobufEnumIndex;
import it.auties.protobuf.model.ProtobufEnum;
import it.auties.protobuf.annotation.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 implements ProtobufEnum {
@ProtobufEnum
public enum ClientType {
/**
* 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>
Expand All @@ -20,6 +21,7 @@ public enum ClientType implements ProtobufEnum {
MOBILE(1);

final int index;

ClientType(@ProtobufEnumIndex int index) {
this.index = index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import it.auties.whatsapp.controller.StoreKeysPair;
import it.auties.whatsapp.model.mobile.PhoneNumber;
import it.auties.whatsapp.model.mobile.SixPartsKeys;
import it.auties.whatsapp.util.Bytes;

import java.util.List;
import java.util.Objects;
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/it/auties/whatsapp/api/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package it.auties.whatsapp.api;

import it.auties.whatsapp.exception.HmacValidationException;
import it.auties.whatsapp.model.jid.Jid;
import it.auties.whatsapp.util.Exceptions;

import java.nio.file.Path;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer;

import static it.auties.whatsapp.api.ErrorHandler.Location.*;
Expand Down Expand Up @@ -64,37 +63,41 @@ static ErrorHandler toFile(Path directory) {
* @param printer a consumer that handles the printing of the throwable, can be null
* @return a non-null error handler
*/
static ErrorHandler defaultErrorHandler(BiConsumer<Whatsapp, Throwable> printer) {
private static ErrorHandler defaultErrorHandler(BiConsumer<Whatsapp, Throwable> printer) {
return (whatsapp, location, throwable) -> {
var logger = System.getLogger("ErrorHandler");
var jid = whatsapp.store()
.jid()
.map(Jid::user)
.orElse("UNKNOWN");
if(location == RECONNECT) {
logger.log(WARNING, "Cannot reconnect: retrying on next timeout");
logger.log(WARNING, "[{0}] Cannot reconnect: retrying on next timeout", jid);
return Result.DISCARD;
}

if(throwable instanceof CompletionException && throwable.getCause() instanceof TimeoutException) {
logger.log(WARNING, "Detected possible network anomaly: reconnecting");
return Result.RECONNECT;
}

logger.log(ERROR, "Socket failure at %s".formatted(location));
logger.log(ERROR, "[{0}] Socket failure at {1}", jid, location);
if (printer != null) {
printer.accept(whatsapp, throwable);
}

if(location == LOGIN) {
logger.log(WARNING, "[{0}] Cannot login", jid);
return Result.DISCONNECT;
}

if (location == CRYPTOGRAPHY && whatsapp.store().clientType() == ClientType.MOBILE) {
logger.log(WARNING, "Reconnecting");
logger.log(WARNING, "[{0}] Reconnecting", jid);
return Result.RECONNECT;
}

if (location == INITIAL_APP_STATE_SYNC
|| location == CRYPTOGRAPHY
|| (location == MESSAGE && throwable instanceof HmacValidationException)) {
logger.log(WARNING, "Restore");
logger.log(WARNING, "[{0}] Restore", jid);
return Result.RESTORE;
}

logger.log(WARNING, "Ignored failure");
logger.log(WARNING, "[{0}] Ignored failure", jid);
return Result.DISCARD;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public sealed class MobileRegistrationBuilder {
public final static class Unregistered extends MobileRegistrationBuilder {
private UnverifiedResult unregisteredResult;
private VerificationCodeMethod verificationCodeMethod;
private boolean autocloseCloudVerificationClient;

Unregistered(Store store, Keys keys, ErrorHandler errorHandler) {
super(store, keys, errorHandler);
Expand Down Expand Up @@ -88,14 +89,15 @@ public CompletableFuture<RegisteredResult> register(long phoneNumber) {
verificationCodeSupplier,
verificationCodeMethod
);
return registration.registerPhoneNumber().thenApply(response -> {
var api = Whatsapp.customBuilder()
.store(store)
.keys(keys)
.errorHandler(errorHandler)
.build();
return this.result = new RegisteredResult(api, Optional.ofNullable(response));
});
return registration.registerPhoneNumber()
.thenApplyAsync(response -> {
var api = Whatsapp.customBuilder()
.store(store)
.keys(keys)
.errorHandler(errorHandler)
.build();
return this.result = new RegisteredResult(api, Optional.ofNullable(response));
});
}

var api = Whatsapp.customBuilder()
Expand All @@ -106,7 +108,6 @@ public CompletableFuture<RegisteredResult> register(long phoneNumber) {
return CompletableFuture.completedFuture(result);
}


/**
* Asks Whatsapp for a one-time-password to start the registration process
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* This interface allows to consume a pairing code sent by WhatsappWeb
*/
@FunctionalInterface
@SuppressWarnings("unused")
public non-sealed interface PairingCodeHandler extends Consumer<String>, WebVerificationHandler {
/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/it/auties/whatsapp/api/QrHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
/**
* This interface allows to consume a qr code and provides default common implementations to do so
*/
@FunctionalInterface
@SuppressWarnings("unused")
public non-sealed interface QrHandler extends Consumer<String>, WebVerificationHandler {
/**
Expand Down Expand Up @@ -134,8 +133,8 @@ static ToFileConsumer toDesktop() {
return;
}
Desktop.getDesktop().open(path.toFile());
} catch (IOException exception) {
throw new UncheckedIOException("Cannot open file with desktop", exception);
} catch (Throwable throwable) {
throw new RuntimeException("Cannot open file with desktop", throwable);
}
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/it/auties/whatsapp/api/TextPreviewSetting.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package it.auties.whatsapp.api;

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

/**
* The constants of this enumerated type describe the various types of text preview that can be
* used
*/
public enum TextPreviewSetting implements ProtobufEnum {
@ProtobufEnum
public enum TextPreviewSetting {
/**
* 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
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/it/auties/whatsapp/api/WebHistoryLength.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package it.auties.whatsapp.api;

import it.auties.protobuf.annotation.ProtobufMessage;
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
*/
@ProtobufMessage
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 STANDARD = new WebHistoryLength(59206);
private static final WebHistoryLength EXTENDED = new WebHistoryLength(Integer.MAX_VALUE);

/**
Expand Down Expand Up @@ -65,6 +65,6 @@ public boolean isZero() {
* @return a boolean
*/
public boolean isExtended() {
return size > Specification.Whatsapp.DEFAULT_HISTORY_SIZE;
return size > STANDARD.size();
}
}
Loading

0 comments on commit 58cf2aa

Please sign in to comment.