Cleanup manager package

This commit is contained in:
AsamK 2022-02-06 17:21:39 +01:00
parent 3040da99c4
commit 5cccf52103
55 changed files with 190 additions and 160 deletions

View file

@ -1,5 +1,7 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.AccountCheckException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.Device;
import org.asamk.signal.manager.api.Group;
@ -9,11 +11,14 @@ import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotRegisteredException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
@ -38,7 +43,6 @@ import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -53,7 +57,7 @@ public interface Manager extends Closeable {
ServiceEnvironment serviceEnvironment,
String userAgent,
TrustNewIdentity trustNewIdentity
) throws IOException, NotRegisteredException {
) throws IOException, NotRegisteredException, AccountCheckException {
var pathConfig = PathConfig.createDefault(settingsPath);
if (!SignalAccount.userExists(pathConfig.dataPath(), number)) {
@ -70,7 +74,16 @@ public interface Manager extends Closeable {
account.initDatabase();
final var serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
return new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
final var manager = new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
try {
manager.checkAccountState();
} catch (IOException e) {
manager.close();
throw new AccountCheckException("Error while checking account " + account + ": " + e.getMessage(), e);
}
return manager;
}
static void initLogger() {
@ -81,26 +94,8 @@ public interface Manager extends Closeable {
return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
}
static List<String> getAllLocalAccountNumbers(File settingsPath) {
var pathConfig = PathConfig.createDefault(settingsPath);
final var dataPath = pathConfig.dataPath();
final var files = dataPath.listFiles();
if (files == null) {
return List.of();
}
return Arrays.stream(files)
.filter(File::isFile)
.map(File::getName)
.filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
.toList();
}
String getSelfNumber();
void checkAccountState() throws IOException;
/**
* This is used for checking a set of phone numbers for registration on Signal
*

View file

@ -16,6 +16,7 @@
*/
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.Device;
import org.asamk.signal.manager.api.Group;
@ -24,6 +25,7 @@ import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults;
@ -31,6 +33,7 @@ import org.asamk.signal.manager.api.SendMessageResult;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
@ -50,6 +53,8 @@ import org.asamk.signal.manager.storage.recipients.Contact;
import org.asamk.signal.manager.storage.recipients.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
import org.asamk.signal.manager.storage.stickers.Sticker;
import org.asamk.signal.manager.util.AttachmentUtils;
import org.asamk.signal.manager.util.KeyUtils;
@ -89,7 +94,7 @@ import java.util.stream.Stream;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
public class ManagerImpl implements Manager {
class ManagerImpl implements Manager {
private final static Logger logger = LoggerFactory.getLogger(ManagerImpl.class);
@ -166,8 +171,7 @@ public class ManagerImpl implements Manager {
return account.getAccount();
}
@Override
public void checkAccountState() throws IOException {
void checkAccountState() throws IOException {
context.getAccountHelper().checkAccountState();
}

View file

@ -1,13 +1,58 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.AccountCheckException;
import org.asamk.signal.manager.api.NotRegisteredException;
import org.asamk.signal.manager.config.ServiceEnvironment;
import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public interface MultiAccountManager extends AutoCloseable {
static MultiAccountManager init(
final File settingsPath,
final ServiceEnvironment serviceEnvironment,
final String userAgent,
final TrustNewIdentity trustNewIdentity
) {
final var logger = LoggerFactory.getLogger(MultiAccountManager.class);
final var managers = getAllLocalAccountNumbers(settingsPath).stream().map(a -> {
try {
return Manager.init(a, settingsPath, serviceEnvironment, userAgent, trustNewIdentity);
} catch (NotRegisteredException | IOException | AccountCheckException e) {
logger.warn("Ignoring {}: {} ({})", a, e.getMessage(), e.getClass().getSimpleName());
return null;
}
}).filter(Objects::nonNull).toList();
return new MultiAccountManagerImpl(managers, settingsPath, serviceEnvironment, userAgent);
}
static List<String> getAllLocalAccountNumbers(File settingsPath) {
var pathConfig = PathConfig.createDefault(settingsPath);
final var dataPath = pathConfig.dataPath();
final var files = dataPath.listFiles();
if (files == null) {
return List.of();
}
return Arrays.stream(files)
.filter(File::isFile)
.map(File::getName)
.filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
.toList();
}
List<String> getAccountNumbers();
List<Manager> getManagers();

View file

@ -17,7 +17,7 @@ import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public class MultiAccountManagerImpl implements MultiAccountManager {
class MultiAccountManagerImpl implements MultiAccountManager {
private final static Logger logger = LoggerFactory.getLogger(MultiAccountManagerImpl.class);
@ -25,19 +25,19 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
private final Set<Consumer<Manager>> onManagerRemovedHandlers = new HashSet<>();
private final Set<Manager> managers = new HashSet<>();
private final Map<URI, ProvisioningManager> provisioningManagers = new HashMap<>();
private final File dataPath;
private final File settingsPath;
private final ServiceEnvironment serviceEnvironment;
private final String userAgent;
public MultiAccountManagerImpl(
final Collection<Manager> managers,
final File dataPath,
final File settingsPath,
final ServiceEnvironment serviceEnvironment,
final String userAgent
) {
this.managers.addAll(managers);
managers.forEach(m -> m.addClosedListener(() -> this.removeManager(m)));
this.dataPath = dataPath;
this.settingsPath = settingsPath;
this.serviceEnvironment = serviceEnvironment;
this.userAgent = userAgent;
}
@ -119,12 +119,12 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
}
private ProvisioningManager getNewProvisioningManager() {
return ProvisioningManager.init(dataPath, serviceEnvironment, userAgent, this::addManager);
return ProvisioningManager.init(settingsPath, serviceEnvironment, userAgent, this::addManager);
}
@Override
public RegistrationManager getNewRegistrationManager(String account) throws IOException {
return RegistrationManager.init(account, dataPath, serviceEnvironment, userAgent, this::addManager);
return RegistrationManager.init(account, settingsPath, serviceEnvironment, userAgent, this::addManager);
}
@Override

View file

@ -2,11 +2,11 @@ package org.asamk.signal.manager;
import java.io.File;
public record PathConfig(
record PathConfig(
File dataPath, File attachmentsPath, File avatarsPath, File stickerPacksPath
) {
public static PathConfig createDefault(final File settingsPath) {
static PathConfig createDefault(final File settingsPath) {
return new PathConfig(new File(settingsPath, "data"),
new File(settingsPath, "attachments"),
new File(settingsPath, "avatars"),

View file

@ -1,5 +1,6 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironment;
@ -32,5 +33,5 @@ public interface ProvisioningManager {
URI getDeviceLinkUri() throws TimeoutException, IOException;
String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExists;
String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException;
}

View file

@ -16,6 +16,7 @@
*/
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.storage.SignalAccount;
@ -38,7 +39,7 @@ import java.net.URI;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
public class ProvisioningManagerImpl implements ProvisioningManager {
class ProvisioningManagerImpl implements ProvisioningManager {
private final static Logger logger = LoggerFactory.getLogger(ProvisioningManagerImpl.class);
@ -87,14 +88,14 @@ public class ProvisioningManagerImpl implements ProvisioningManager {
}
@Override
public String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExists {
public String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException {
var ret = accountManager.getNewDeviceRegistration(tempIdentityKey);
var number = ret.getNumber();
logger.info("Received link information from {}, linking in progress ...", number);
if (SignalAccount.userExists(pathConfig.dataPath(), number) && !canRelinkExistingAccount(number)) {
throw new UserAlreadyExists(number, SignalAccount.getFileName(pathConfig.dataPath(), number));
throw new UserAlreadyExistsException(number, SignalAccount.getFileName(pathConfig.dataPath(), number));
}
var encryptedDeviceName = deviceName == null

View file

@ -47,7 +47,7 @@ import java.util.function.Consumer;
import static org.asamk.signal.manager.config.ServiceConfig.capabilities;
public class RegistrationManagerImpl implements RegistrationManager {
class RegistrationManagerImpl implements RegistrationManager {
private final static Logger logger = LoggerFactory.getLogger(RegistrationManagerImpl.class);

View file

@ -52,7 +52,7 @@ public class SignalDependencies {
private ProfileService profileService;
private SignalServiceCipher cipher;
public SignalDependencies(
SignalDependencies(
final ServiceEnvironmentConfig serviceEnvironmentConfig,
final String userAgent,
final DynamicCredentialsProvider credentialsProvider,

View file

@ -21,7 +21,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
* The monitor is also responsible for sending heartbeats/keep-alive messages to prevent
* timeouts.
*/
public final class SignalWebSocketHealthMonitor implements HealthMonitor {
final class SignalWebSocketHealthMonitor implements HealthMonitor {
private final static Logger logger = LoggerFactory.getLogger(SignalWebSocketHealthMonitor.class);

View file

@ -0,0 +1,12 @@
package org.asamk.signal.manager.api;
public class AccountCheckException extends Exception {
public AccountCheckException(String message) {
super(message);
}
public AccountCheckException(String message, Exception e) {
super(message, e);
}
}

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class AttachmentInvalidException extends Exception {

View file

@ -1,6 +1,5 @@
package org.asamk.signal.manager.api;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.whispersystems.libsignal.IdentityKey;

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class NotMasterDeviceException extends Exception {

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class NotRegisteredException extends Exception {

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
public class StickerPackInvalidException extends Exception {

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
import org.whispersystems.signalservice.internal.storage.protos.ContactRecord;

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;

View file

@ -1,13 +1,13 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.api;
import java.io.File;
public class UserAlreadyExists extends Exception {
public class UserAlreadyExistsException extends Exception {
private final String number;
private final File fileName;
public UserAlreadyExists(String number, File fileName) {
public UserAlreadyExistsException(String number, File fileName) {
this.number = number;
this.fileName = fileName;
}

View file

@ -1,8 +1,8 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.AttachmentStore;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.util.AttachmentUtils;
import org.asamk.signal.manager.util.IOUtils;

View file

@ -4,7 +4,7 @@ import org.asamk.signal.manager.AttachmentStore;
import org.asamk.signal.manager.AvatarStore;
import org.asamk.signal.manager.JobExecutor;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.StickerPackStore;
import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
import org.asamk.signal.manager.storage.SignalAccount;
import java.util.function.Supplier;

View file

@ -1,7 +1,7 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.SendGroupMessageResults;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.util.Utils;

View file

@ -2,8 +2,8 @@ package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.actions.HandleAction;
import org.asamk.signal.manager.actions.RefreshPreKeysAction;
import org.asamk.signal.manager.actions.RenewSessionAction;

View file

@ -2,7 +2,7 @@ package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.actions.HandleAction;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.messageCache.CachedMessage;

View file

@ -1,10 +1,10 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.JsonStickerPack;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.asamk.signal.manager.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View file

@ -1,7 +1,7 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.PhoneNumberSharingMode;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.storage.SignalAccount;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.groups.GroupInfoV1;
import org.asamk.signal.manager.storage.recipients.Contact;

View file

@ -3,7 +3,7 @@ package org.asamk.signal.manager.storage;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.storage.configuration.ConfigurationStore;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.storage.identities;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.whispersystems.libsignal.IdentityKey;

View file

@ -2,7 +2,7 @@ package org.asamk.signal.manager.storage.identities;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.storage.recipients.RecipientResolver;
import org.asamk.signal.manager.util.IOUtils;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.storage.protocol;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.whispersystems.libsignal.IdentityKey;

View file

@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.asamk.signal.manager.TrustLevel;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.Utils;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.slf4j.Logger;

View file

@ -1,4 +1,4 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.storage.stickerPacks;
import org.asamk.signal.manager.api.StickerPack;

View file

@ -1,8 +1,9 @@
package org.asamk.signal.manager;
package org.asamk.signal.manager.storage.stickerPacks;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.asamk.signal.manager.util.IOUtils;
import org.asamk.signal.manager.util.Utils;
import org.whispersystems.signalservice.api.util.StreamDetails;

View file

@ -1,6 +1,6 @@
package org.asamk.signal.manager.util;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;

View file

@ -2,9 +2,9 @@ package org.asamk.signal.manager.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asamk.signal.manager.JsonStickerPack;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceStickerManifestUpload;

View file

@ -22,10 +22,11 @@ import org.asamk.signal.dbus.DbusMultiAccountManagerImpl;
import org.asamk.signal.dbus.DbusProvisioningManagerImpl;
import org.asamk.signal.dbus.DbusRegistrationManagerImpl;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManagerImpl;
import org.asamk.signal.manager.NotRegisteredException;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.api.AccountCheckException;
import org.asamk.signal.manager.api.NotRegisteredException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironment;
import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
@ -46,8 +47,6 @@ import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS;
@ -143,18 +142,18 @@ public class App {
return;
}
final File dataPath;
var config = ns.getString("config");
if (config != null) {
dataPath = new File(config);
} else {
dataPath = getDefaultDataPath();
}
if (!ServiceConfig.isSignalClientAvailable()) {
throw new UserErrorException("Missing required native library dependency: libsignal-client");
}
final File configPath;
var config = ns.getString("config");
if (config != null) {
configPath = new File(config);
} else {
configPath = getDefaultConfigPath();
}
final var serviceEnvironmentCli = ns.<ServiceEnvironmentCli>get("service-environment");
final var serviceEnvironment = serviceEnvironmentCli == ServiceEnvironmentCli.LIVE
? ServiceEnvironment.LIVE
@ -170,23 +169,21 @@ public class App {
throw new UserErrorException("You cannot specify a account (phone number) when linking");
}
handleProvisioningCommand(provisioningCommand, dataPath, serviceEnvironment, outputWriter);
handleProvisioningCommand(provisioningCommand, configPath, serviceEnvironment, outputWriter);
return;
}
if (account == null) {
var accounts = Manager.getAllLocalAccountNumbers(dataPath);
if (command instanceof MultiLocalCommand multiLocalCommand) {
handleMultiLocalCommand(multiLocalCommand,
dataPath,
configPath,
serviceEnvironment,
accounts,
outputWriter,
trustNewIdentity);
return;
}
var accounts = MultiAccountManager.getAllLocalAccountNumbers(configPath);
if (accounts.size() == 0) {
throw new UserErrorException("No local users found, you first need to register or link an account");
} else if (accounts.size() > 1) {
@ -200,7 +197,7 @@ public class App {
}
if (command instanceof RegistrationCommand registrationCommand) {
handleRegistrationCommand(registrationCommand, account, dataPath, serviceEnvironment);
handleRegistrationCommand(registrationCommand, account, configPath, serviceEnvironment);
return;
}
@ -210,7 +207,7 @@ public class App {
handleLocalCommand((LocalCommand) command,
account,
dataPath,
configPath,
serviceEnvironment,
outputWriter,
trustNewIdentity);
@ -218,11 +215,11 @@ public class App {
private void handleProvisioningCommand(
final ProvisioningCommand command,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final OutputWriter outputWriter
) throws CommandException {
var pm = ProvisioningManager.init(dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
var pm = ProvisioningManager.init(configPath, serviceEnvironment, BaseConfig.USER_AGENT);
command.handleCommand(ns, pm, outputWriter);
}
@ -245,12 +242,12 @@ public class App {
private void handleRegistrationCommand(
final RegistrationCommand command,
final String account,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment
) throws CommandException {
final RegistrationManager manager;
try {
manager = RegistrationManager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT);
manager = RegistrationManager.init(account, configPath, serviceEnvironment, BaseConfig.USER_AGENT);
} catch (Throwable e) {
throw new UnexpectedErrorException("Error loading or creating state file: "
+ e.getMessage()
@ -283,12 +280,12 @@ public class App {
private void handleLocalCommand(
final LocalCommand command,
final String account,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final OutputWriter outputWriter,
final TrustNewIdentity trustNewIdentity
) throws CommandException {
try (var m = loadManager(account, dataPath, serviceEnvironment, trustNewIdentity)) {
try (var m = loadManager(account, configPath, serviceEnvironment, trustNewIdentity)) {
command.handleCommand(ns, m, outputWriter);
} catch (IOException e) {
logger.warn("Cleanup failed", e);
@ -313,25 +310,15 @@ public class App {
private void handleMultiLocalCommand(
final MultiLocalCommand command,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final List<String> accounts,
final OutputWriter outputWriter,
final TrustNewIdentity trustNewIdentity
) throws CommandException {
final var managers = new ArrayList<Manager>();
for (String a : accounts) {
try {
managers.add(loadManager(a, dataPath, serviceEnvironment, trustNewIdentity));
} catch (CommandException e) {
logger.warn("Ignoring {}: {}", a, e.getMessage());
}
}
try (var multiAccountManager = new MultiAccountManagerImpl(managers,
dataPath,
try (var multiAccountManager = MultiAccountManager.init(configPath,
serviceEnvironment,
BaseConfig.USER_AGENT)) {
BaseConfig.USER_AGENT,
trustNewIdentity)) {
command.handleCommand(ns, multiAccountManager, outputWriter);
}
}
@ -351,16 +338,22 @@ public class App {
private Manager loadManager(
final String account,
final File dataPath,
final File configPath,
final ServiceEnvironment serviceEnvironment,
final TrustNewIdentity trustNewIdentity
) throws CommandException {
Manager manager;
logger.trace("Loading account file for {}", account);
try {
manager = Manager.init(account, dataPath, serviceEnvironment, BaseConfig.USER_AGENT, trustNewIdentity);
return Manager.init(account, configPath, serviceEnvironment, BaseConfig.USER_AGENT, trustNewIdentity);
} catch (NotRegisteredException e) {
throw new UserErrorException("User " + account + " is not registered.");
} catch (AccountCheckException ace) {
if (ace.getCause() instanceof IOException e) {
throw new IOErrorException("Error while checking account " + account + ": " + e.getMessage(), e);
} else {
throw new UnexpectedErrorException("Error while checking account " + account + ": " + ace.getMessage(),
ace);
}
} catch (Throwable e) {
throw new UnexpectedErrorException("Error loading state file for user "
+ account
@ -370,20 +363,6 @@ public class App {
+ e.getClass().getSimpleName()
+ ")", e);
}
logger.trace("Checking account state");
try {
manager.checkAccountState();
} catch (IOException e) {
try {
manager.close();
} catch (IOException ie) {
logger.warn("Failed to close broken account", ie);
}
throw new IOErrorException("Error while checking account " + account + ": " + e.getMessage(), e);
}
return manager;
}
private void initDbusClient(
@ -457,7 +436,7 @@ public class App {
/**
* @return the default data directory to be used by signal-cli.
*/
private static File getDefaultDataPath() {
private static File getDefaultConfigPath() {
return new File(IOUtils.getDataHomeDir(), "signal-cli");
}
}

View file

@ -1,7 +1,7 @@
package org.asamk.signal;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupId;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.output.OutputWriter;

View file

@ -6,7 +6,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.output.JsonWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,7 +56,7 @@ public class FinishLinkCommand implements JsonRpcMultiCommand<FinishLinkCommand.
throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage(), e);
} catch (UserAlreadyExists e) {
} catch (UserAlreadyExistsException e) {
throw new UserErrorException("The user "
+ e.getNumber()
+ " already exists\nDelete \""

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.slf4j.Logger;
@ -49,7 +49,7 @@ public class LinkCommand implements ProvisioningCommand {
throw new UserErrorException("Link request timed out, please try again.");
} catch (IOException e) {
throw new IOErrorException("Link request error: " + e.getMessage(), e);
} catch (UserAlreadyExists e) {
} catch (UserAlreadyExistsException e) {
throw new UserErrorException("The user "
+ e.getNumber()
+ " already exists\nDelete \""

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;

View file

@ -7,8 +7,8 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.RecipientIdentifier;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.output.OutputWriter;

View file

@ -7,8 +7,8 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.output.OutputWriter;
import java.io.IOException;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.util.CommandUtil;

View file

@ -7,8 +7,8 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.UnregisteredRecipientException;
import org.asamk.signal.manager.api.UpdateGroup;

View file

@ -7,7 +7,7 @@ import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;

View file

@ -2,10 +2,9 @@ package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.signal.DbusConfig;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Configuration;
import org.asamk.signal.manager.api.Device;
import org.asamk.signal.manager.api.Group;
@ -14,6 +13,7 @@ import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults;
@ -84,11 +84,6 @@ public class DbusManagerImpl implements Manager {
return signal.getSelfNumber();
}
@Override
public void checkAccountState() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Pair<String, UUID>> areUsersRegistered(final Set<String> numbers) throws IOException {
final var numbersList = new ArrayList<>(numbers);

View file

@ -2,7 +2,7 @@ package org.asamk.signal.dbus;
import org.asamk.SignalControl;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import java.io.IOException;
@ -27,9 +27,7 @@ public class DbusProvisioningManagerImpl implements ProvisioningManager {
}
public DbusProvisioningManagerImpl(
final SignalControl signalControl,
DBusConnection connection,
URI deviceLinkUri
final SignalControl signalControl, DBusConnection connection, URI deviceLinkUri
) {
this.signalControl = signalControl;
this.connection = connection;
@ -47,7 +45,7 @@ public class DbusProvisioningManagerImpl implements ProvisioningManager {
}
@Override
public String finishDeviceLink(final String deviceName) throws IOException, TimeoutException, UserAlreadyExists {
public String finishDeviceLink(final String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException {
return signalControl.finishLink(deviceLinkUri.toString(), deviceName);
}
}

View file

@ -7,7 +7,7 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.MultiAccountManager;
import org.asamk.signal.manager.ProvisioningManager;
import org.asamk.signal.manager.RegistrationManager;
import org.asamk.signal.manager.UserAlreadyExists;
import org.asamk.signal.manager.api.UserAlreadyExistsException;
import org.asamk.signal.manager.api.CaptchaRequiredException;
import org.asamk.signal.manager.api.IncorrectPinException;
import org.asamk.signal.manager.api.PinLockedException;
@ -100,7 +100,7 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
final ProvisioningManager provisioningManager = c.getProvisioningManagerFor(deviceLinkUri);
try {
provisioningManager.finishDeviceLink(newDeviceName);
} catch (IOException | TimeoutException | UserAlreadyExists e) {
} catch (IOException | TimeoutException | UserAlreadyExistsException e) {
e.printStackTrace();
}
});
@ -127,7 +127,7 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
try {
final var provisioningManager = c.getProvisioningManagerFor(new URI(deviceLinkUri));
return provisioningManager.finishDeviceLink(newDeviceName);
} catch (TimeoutException | IOException | UserAlreadyExists | URISyntaxException e) {
} catch (TimeoutException | IOException | UserAlreadyExistsException | URISyntaxException e) {
throw new SignalControl.Error.Failure(e.getClass().getSimpleName() + " " + e.getMessage());
}
}

View file

@ -2,16 +2,16 @@ package org.asamk.signal.dbus;
import org.asamk.Signal;
import org.asamk.signal.BaseConfig;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.AttachmentInvalidException;
import org.asamk.signal.manager.api.Identity;
import org.asamk.signal.manager.api.InactiveGroupLinkException;
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
import org.asamk.signal.manager.api.InvalidNumberException;
import org.asamk.signal.manager.api.InvalidStickerException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.NotMasterDeviceException;
import org.asamk.signal.manager.api.Pair;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendMessageResult;

View file

@ -3,7 +3,7 @@ package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.UntrustedIdentityException;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;