tdlight-java/tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java

398 lines
14 KiB
Java
Raw Normal View History

2021-09-27 19:27:13 +02:00
package it.tdlight.client;
2023-05-10 18:01:35 +02:00
import io.atlassian.util.concurrent.CopyOnWriteMap;
2023-04-27 02:14:39 +02:00
import it.tdlight.ClientFactory;
import it.tdlight.ExceptionHandler;
import it.tdlight.Init;
import it.tdlight.ResultHandler;
import it.tdlight.TelegramClient;
import it.tdlight.jni.TdApi.Message;
2023-04-27 02:14:39 +02:00
import it.tdlight.jni.TdApi.Update;
2023-05-10 10:12:43 +02:00
import it.tdlight.util.UnsupportedNativeLibraryException;
2021-09-27 19:27:13 +02:00
import it.tdlight.jni.TdApi;
2022-07-12 17:53:52 +02:00
import it.tdlight.jni.TdApi.ChatListArchive;
import it.tdlight.jni.TdApi.ChatListMain;
2021-09-27 19:27:13 +02:00
import it.tdlight.jni.TdApi.Function;
import it.tdlight.jni.TdApi.User;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.Map;
2023-03-12 15:07:55 +01:00
import java.util.Objects;
2021-09-27 19:27:13 +02:00
import java.util.Set;
2023-04-27 02:14:39 +02:00
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
2021-09-27 19:27:13 +02:00
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
2021-10-24 01:03:16 +02:00
import java.util.concurrent.RejectedExecutionException;
2021-09-27 19:27:13 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-05-10 18:01:35 +02:00
import static it.tdlight.util.MapUtils.addAllKeys;
2021-09-27 19:27:13 +02:00
@SuppressWarnings("unused")
2023-04-27 02:14:39 +02:00
public final class SimpleTelegramClient implements Authenticable, MutableTelegramClient {
2021-09-27 19:27:13 +02:00
public static final Logger LOG = LoggerFactory.getLogger(SimpleTelegramClient.class);
2021-09-27 19:27:13 +02:00
static {
try {
2023-05-10 10:12:43 +02:00
Init.init();
} catch (UnsupportedNativeLibraryException e) {
2021-09-27 19:27:13 +02:00
throw new RuntimeException("Can't load native libraries", e);
}
}
private final TelegramClient client;
2023-04-27 02:14:39 +02:00
private ClientInteraction clientInteraction;
2021-09-27 19:27:13 +02:00
private final TDLibSettings settings;
private final AuthenticationSupplier<?> authenticationData;
2021-09-27 19:27:13 +02:00
2023-05-10 18:01:35 +02:00
private final CopyOnWriteMap<String, CopyOnWriteMap<CommandHandler, Void>> commandHandlers = CopyOnWriteMap.newHashMap();
private final CopyOnWriteMap<ResultHandler<TdApi.Update>, Void> updateHandlers = CopyOnWriteMap.newHashMap();
private final CopyOnWriteMap<ExceptionHandler, Void> updateExceptionHandlers = CopyOnWriteMap.newHashMap();
private final CopyOnWriteMap<ExceptionHandler, Void> defaultExceptionHandlers = CopyOnWriteMap.newHashMap();
2021-09-27 19:27:13 +02:00
2022-07-12 17:53:52 +02:00
private final AuthorizationStateReadyGetMe meGetter;
private final AuthorizationStateReadyLoadChats mainChatsLoader;
private final AuthorizationStateReadyLoadChats archivedChatsLoader;
private final CompletableFuture<Void> closed = new CompletableFuture<>();
private final ConcurrentMap<TemporaryMessageURL, CompletableFuture<Message>> temporaryMessages = new ConcurrentHashMap<>();
2021-09-27 19:27:13 +02:00
2023-04-27 02:14:39 +02:00
SimpleTelegramClient(ClientFactory clientFactory,
TDLibSettings settings,
Map<String, Set<CommandHandler>> commandHandlers,
Set<ResultHandler<Update>> updateHandlers,
Set<ExceptionHandler> updateExceptionHandlers,
Set<ExceptionHandler> defaultExceptionHandlers,
ClientInteraction clientInteraction,
AuthenticationSupplier<?> authenticationData) {
2023-04-27 02:14:39 +02:00
this.client = clientFactory.createClient();
this.settings = Objects.requireNonNull(settings, "TDLight client settings are null");
2023-05-10 18:01:35 +02:00
commandHandlers.forEach((k, v) -> {
CopyOnWriteMap<CommandHandler, Void> mapped = CopyOnWriteMap.newHashMap();
addAllKeys(mapped, v, null);
this.commandHandlers.put(k, mapped);
});
addAllKeys(this.updateHandlers, updateHandlers, null);
addAllKeys(this.updateExceptionHandlers, updateExceptionHandlers, null);
addAllKeys(this.defaultExceptionHandlers, defaultExceptionHandlers, null);
2023-05-10 15:26:11 +02:00
this.clientInteraction = clientInteraction != null ? clientInteraction
: new ScannerClientInteraction(SequentialRequestsExecutor.getInstance(), this);
2023-04-27 02:14:39 +02:00
2021-09-27 19:27:13 +02:00
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
2021-10-22 12:54:28 +02:00
new AuthorizationStateWaitTdlibParametersHandler(client, settings, this::handleDefaultException)
);
2021-09-27 19:27:13 +02:00
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
2021-10-22 12:54:28 +02:00
new AuthorizationStateWaitAuthenticationDataHandler(client, this, this::handleDefaultException)
);
2021-09-27 19:27:13 +02:00
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
2021-10-22 12:54:28 +02:00
new AuthorizationStateWaitRegistrationHandler(client,
2023-04-27 02:14:39 +02:00
new SimpleTelegramClientInteraction(),
2021-10-22 12:54:28 +02:00
this::handleDefaultException
)
);
2021-09-27 19:27:13 +02:00
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
2021-10-22 12:54:28 +02:00
new AuthorizationStateWaitPasswordHandler(client,
2023-04-27 02:14:39 +02:00
new SimpleTelegramClientInteraction(),
2021-10-22 12:54:28 +02:00
this::handleDefaultException
)
);
2021-09-27 19:27:13 +02:00
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
2023-04-27 02:14:39 +02:00
new AuthorizationStateWaitOtherDeviceConfirmationHandler(new SimpleTelegramClientInteraction(),
this::handleDefaultException
)
2021-10-22 12:54:28 +02:00
);
2021-09-27 19:27:13 +02:00
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
2021-10-22 12:54:28 +02:00
new AuthorizationStateWaitCodeHandler(client,
2023-04-27 02:14:39 +02:00
new SimpleTelegramClientInteraction(),
2023-05-14 23:05:30 +02:00
getTestCode(authenticationData),
2021-10-22 12:54:28 +02:00
this::handleDefaultException
)
);
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class, new AuthorizationStateWaitForExit(this::onCloseUpdate));
2022-07-12 17:53:52 +02:00
this.mainChatsLoader = new AuthorizationStateReadyLoadChats(client, new ChatListMain());
this.archivedChatsLoader = new AuthorizationStateReadyLoadChats(client, new ChatListArchive());
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
this.meGetter = new AuthorizationStateReadyGetMe(client, mainChatsLoader, archivedChatsLoader));
this.addUpdateHandler(TdApi.UpdateNewMessage.class, new CommandsHandler(client, this.commandHandlers, this::getMe));
2023-08-30 11:51:06 +02:00
TemporaryMessageHandler temporaryMessageHandler = new TemporaryMessageHandler(this.temporaryMessages);
this.addUpdateHandler(TdApi.UpdateMessageSendSucceeded.class, temporaryMessageHandler);
this.addUpdateHandler(TdApi.UpdateMessageSendFailed.class, temporaryMessageHandler);
this.authenticationData = authenticationData;
createDirectories();
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
2021-09-27 19:27:13 +02:00
}
2023-05-14 23:05:30 +02:00
private String getTestCode(AuthenticationSupplier<?> authenticationData) {
if (authenticationData instanceof AuthenticationDataImpl) {
if (!((AuthenticationDataImpl) authenticationData).isBot()
&& ((AuthenticationDataImpl) authenticationData).isTest()) {
String phoneNumber = ((AuthenticationDataImpl) authenticationData).getUserPhoneNumber();
2023-05-14 23:19:51 +02:00
String loginCodeChar = phoneNumber.substring(5, 6);
StringBuilder sb = new StringBuilder();
//noinspection StringRepeatCanBeUsed
for (int i = 0; i < 5; i++) {
sb.append(loginCodeChar);
}
return sb.toString();
2023-05-14 23:05:30 +02:00
}
}
return null;
}
2021-09-27 19:27:13 +02:00
private void handleUpdate(TdApi.Object update) {
boolean handled = false;
2023-05-10 18:01:35 +02:00
for (ResultHandler<TdApi.Update> updateHandler : updateHandlers.keySet()) {
2021-09-27 19:27:13 +02:00
updateHandler.onResult(update);
handled = true;
}
if (!handled) {
LOG.warn("An update was not handled, please use addUpdateHandler(handler) before starting the client!");
2021-09-27 19:27:13 +02:00
}
}
private void handleUpdateException(Throwable ex) {
boolean handled = false;
2023-05-10 18:01:35 +02:00
for (ExceptionHandler updateExceptionHandler : updateExceptionHandlers.keySet()) {
2021-09-27 19:27:13 +02:00
updateExceptionHandler.onException(ex);
handled = true;
}
if (!handled) {
LOG.warn("Error received from Telegram!", ex);
2021-09-27 19:27:13 +02:00
}
}
private void handleDefaultException(Throwable ex) {
boolean handled = false;
2023-05-10 18:01:35 +02:00
for (ExceptionHandler exceptionHandler : defaultExceptionHandlers.keySet()) {
2021-09-27 19:27:13 +02:00
exceptionHandler.onException(ex);
handled = true;
}
if (!handled) {
LOG.warn("Unhandled exception!", ex);
2021-09-27 19:27:13 +02:00
}
}
private void handleResultHandlingException(Throwable ex) {
LOG.error("Failed to handle the request result", ex);
}
2021-09-27 19:27:13 +02:00
@Override
2023-04-27 02:14:39 +02:00
public AuthenticationSupplier<?> getAuthenticationSupplier() {
return authenticationData;
2021-09-27 19:27:13 +02:00
}
2023-04-27 02:14:39 +02:00
@Override
2021-09-27 19:27:13 +02:00
public void setClientInteraction(ClientInteraction clientInteraction) {
this.clientInteraction = clientInteraction;
}
2023-04-27 02:14:39 +02:00
@Override
2021-09-27 19:27:13 +02:00
public <T extends TdApi.Update> void addCommandHandler(String commandName, CommandHandler handler) {
2023-05-10 18:01:35 +02:00
CopyOnWriteMap<CommandHandler, Void> handlers = this.commandHandlers.computeIfAbsent(commandName, k -> CopyOnWriteMap.newHashMap());
handlers.put(handler, null);
2021-09-27 19:27:13 +02:00
}
2023-04-27 02:14:39 +02:00
@Override
public <T extends TdApi.Update> void addUpdateHandler(Class<T> updateType, GenericUpdateHandler<? super T> handler) {
2023-05-10 18:01:35 +02:00
this.updateHandlers.put(new SimpleResultHandler<>(updateType, handler), null);
2021-09-27 19:27:13 +02:00
}
2023-04-27 02:14:39 +02:00
@Override
2021-09-27 19:27:13 +02:00
public void addUpdatesHandler(GenericUpdateHandler<TdApi.Update> handler) {
2023-05-10 18:01:35 +02:00
this.updateHandlers.put(new SimpleUpdateHandler(handler, LOG), null);
2021-09-27 19:27:13 +02:00
}
/**
* Optional handler to handle errors received from TDLib
*/
2023-04-27 02:14:39 +02:00
@Override
2021-09-27 19:27:13 +02:00
public void addUpdateExceptionHandler(ExceptionHandler updateExceptionHandler) {
2023-05-10 18:01:35 +02:00
this.updateExceptionHandlers.put(updateExceptionHandler, null);
2021-09-27 19:27:13 +02:00
}
/**
* Optional handler to handle uncaught errors (when using send without an appropriate error handler)
*/
2023-04-27 02:14:39 +02:00
@Override
2021-09-27 19:27:13 +02:00
public void addDefaultExceptionHandler(ExceptionHandler defaultExceptionHandlers) {
2023-05-10 18:01:35 +02:00
this.defaultExceptionHandlers.put(defaultExceptionHandlers, null);
2021-09-27 19:27:13 +02:00
}
private void createDirectories() {
try {
if (Files.notExists(settings.getDatabaseDirectoryPath())) {
Files.createDirectories(settings.getDatabaseDirectoryPath());
}
if (Files.notExists(settings.getDownloadedFilesDirectoryPath())) {
Files.createDirectories(settings.getDownloadedFilesDirectoryPath());
}
} catch (IOException ex) {
throw new UncheckedIOException("Can't create TDLight directories", ex);
}
}
/**
2023-03-12 15:07:55 +01:00
* Sends a request to TDLib and get the result.
*
* @param function The request to TDLib.
* @param resultHandler Result handler. If it is null, nothing will be called.
* @throws NullPointerException if function is null.
2021-09-27 19:27:13 +02:00
*/
public <R extends TdApi.Object> void send(TdApi.Function<R> function, GenericResultHandler<R> resultHandler) {
2023-03-12 15:10:38 +01:00
client.send(function, result -> resultHandler.onResult(Result.of(result)), this::handleResultHandlingException);
2023-03-12 15:07:55 +01:00
}
/**
* Sends a request to TDLib and get the result.
*
* @param function The request to TDLib.
* @param resultHandler Result handler. If it is null, nothing will be called.
* @param resultHandlerExceptionHandler Handle exceptions thrown inside the result handler.
* If it is null, the default exception handler will be called.
* @throws NullPointerException if function is null.
*/
public <R extends TdApi.Object> void send(TdApi.Function<R> function, GenericResultHandler<R> resultHandler,
ExceptionHandler resultHandlerExceptionHandler) {
2023-03-12 15:10:38 +01:00
if (resultHandlerExceptionHandler == null) {
resultHandlerExceptionHandler = this::handleResultHandlingException;
}
client.send(function, result -> resultHandler.onResult(Result.of(result)), resultHandlerExceptionHandler);
2021-09-27 19:27:13 +02:00
}
/**
* Sends a request to TDLib and get the result.
*
* @param function The request to TDLib.
* @throws NullPointerException if function is null.
*/
@SuppressWarnings("unchecked")
public <R extends TdApi.Object> CompletableFuture<R> send(TdApi.Function<R> function) {
CompletableFuture<R> future = new CompletableFuture<>();
client.send(function, result -> {
if (result instanceof TdApi.Error) {
future.completeExceptionally(new TelegramError((TdApi.Error) result));
} else {
future.complete((R) result);
}
}, future::completeExceptionally);
return future;
}
/**
* Sends a message
*
* @param function The request to TDLib.
* @param wait Wait until the message has been sent.
* If false, the returned message will not represent the real message, but it will be a temporary message.
* @throws NullPointerException if function is null.
*/
public CompletableFuture<TdApi.Message> sendMessage(TdApi.SendMessage function, boolean wait) {
return this.send(function).thenCompose(msg -> {
CompletableFuture<TdApi.Message> future = new CompletableFuture<>();
CompletableFuture<?> prev = temporaryMessages.put(new TemporaryMessageURL(msg.chatId, msg.id), future);
if (prev != null) {
prev.completeExceptionally(new IllegalStateException("Another temporary message has the same id"));
}
return future;
});
}
2021-09-27 19:27:13 +02:00
/**
* Execute a synchronous function.
* <strong>Please note that only some functions can be executed using this method.</strong>
* If you want to execute a function please use {@link #send(Function, GenericResultHandler)}!
*/
public <R extends TdApi.Object> Result<R> execute(TdApi.Function<R> function) {
2021-09-27 19:27:13 +02:00
return Result.of(client.execute(function));
}
/**
* Send the close signal but don't wait
*/
public CompletableFuture<Void> close() {
return this.send(new TdApi.Close()).thenCompose(x -> this.waitForExitAsync());
}
2021-09-27 19:27:13 +02:00
/**
* Send the close signal but don't wait
*/
public void sendClose() {
2021-09-27 20:22:57 +02:00
client.send(new TdApi.Close(), ok -> {
2021-10-22 13:33:03 +02:00
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
throw new TelegramError((TdApi.Error) ok);
2021-09-27 20:22:57 +02:00
}
});
2021-09-27 19:27:13 +02:00
}
/**
* Send the close signal and wait for exit
*/
public void closeAndWait() throws InterruptedException {
2021-09-27 20:22:57 +02:00
client.send(new TdApi.Close(), ok -> {
2021-10-22 13:33:03 +02:00
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
throw new TelegramError((TdApi.Error) ok);
2021-09-27 20:22:57 +02:00
}
});
2021-09-27 19:27:13 +02:00
this.waitForExit();
}
/**
* Wait until TDLight is closed
*/
public void waitForExit() throws InterruptedException {
try {
closed.get();
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
/**
* Wait until TDLight is closed
*/
public CompletableFuture<Void> waitForExitAsync() {
return closed.copy();
}
private void onCloseUpdate() {
this.closed.complete(null);
this.temporaryMessages.clear();
2021-09-27 19:27:13 +02:00
}
private final class SimpleTelegramClientInteraction implements ClientInteraction {
2023-04-27 02:14:39 +02:00
public SimpleTelegramClientInteraction() {
2021-10-24 01:03:16 +02:00
}
2021-09-27 19:27:13 +02:00
@Override
2023-04-27 02:14:39 +02:00
public CompletableFuture<String> onParameterRequest(InputParameter parameter, ParameterInfo parameterInfo) {
2021-10-24 01:03:16 +02:00
try {
2023-04-27 02:14:39 +02:00
return clientInteraction.onParameterRequest(parameter, parameterInfo);
2021-10-24 01:03:16 +02:00
} catch (RejectedExecutionException | NullPointerException ex) {
LOG.error("Failed to execute onParameterRequest. Returning an empty string", ex);
2023-04-27 02:14:39 +02:00
return CompletableFuture.completedFuture("");
2021-10-24 01:03:16 +02:00
}
2021-09-27 19:27:13 +02:00
}
}
2022-07-12 17:53:52 +02:00
public User getMe() {
return meGetter.getMe();
}
public boolean isMainChatsListLoaded() {
return mainChatsLoader.isLoaded();
}
public boolean isArchivedChatsListLoaded() {
return archivedChatsLoader.isLoaded();
}
2021-09-27 19:27:13 +02:00
}