mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Implement unregister command for jsonrpc and dbus daemon
This commit is contained in:
parent
b78573021d
commit
c73c58723c
11 changed files with 98 additions and 9 deletions
|
@ -13,6 +13,9 @@
|
||||||
{
|
{
|
||||||
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AT\\E"
|
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AT\\E"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AZ\\E"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BB\\E"
|
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BB\\E"
|
||||||
},
|
},
|
||||||
|
|
|
@ -248,6 +248,8 @@ public interface Manager extends Closeable {
|
||||||
|
|
||||||
boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient);
|
boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient);
|
||||||
|
|
||||||
|
void addClosedListener(Runnable listener);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void close() throws IOException;
|
void close() throws IOException;
|
||||||
|
|
||||||
|
|
|
@ -95,6 +95,7 @@ import java.net.URISyntaxException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.SignatureException;
|
import java.security.SignatureException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -142,6 +143,7 @@ public class ManagerImpl implements Manager {
|
||||||
private Thread receiveThread;
|
private Thread receiveThread;
|
||||||
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
|
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
|
||||||
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
|
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
|
||||||
|
private final List<Runnable> closedListeners = new ArrayList<>();
|
||||||
private boolean isReceivingSynchronous;
|
private boolean isReceivingSynchronous;
|
||||||
|
|
||||||
ManagerImpl(
|
ManagerImpl(
|
||||||
|
@ -385,6 +387,7 @@ public class ManagerImpl implements Manager {
|
||||||
dependencies.getAccountManager().setGcmId(Optional.absent());
|
dependencies.getAccountManager().setGcmId(Optional.absent());
|
||||||
|
|
||||||
account.setRegistered(false);
|
account.setRegistered(false);
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -399,6 +402,7 @@ public class ManagerImpl implements Manager {
|
||||||
dependencies.getAccountManager().deleteAccount();
|
dependencies.getAccountManager().deleteAccount();
|
||||||
|
|
||||||
account.setRegistered(false);
|
account.setRegistered(false);
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1325,6 +1329,13 @@ public class ManagerImpl implements Manager {
|
||||||
return identityHelper.trustIdentityAllKeys(recipientId);
|
return identityHelper.trustIdentityAllKeys(recipientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addClosedListener(final Runnable listener) {
|
||||||
|
synchronized (closedListeners) {
|
||||||
|
closedListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void handleIdentityFailure(
|
private void handleIdentityFailure(
|
||||||
final RecipientId recipientId,
|
final RecipientId recipientId,
|
||||||
final org.whispersystems.signalservice.api.messages.SendMessageResult.IdentityFailure identityFailure
|
final org.whispersystems.signalservice.api.messages.SendMessageResult.IdentityFailure identityFailure
|
||||||
|
@ -1390,10 +1401,6 @@ public class ManagerImpl implements Manager {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
close(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void close(boolean closeAccount) throws IOException {
|
|
||||||
Thread thread;
|
Thread thread;
|
||||||
synchronized (messageHandlers) {
|
synchronized (messageHandlers) {
|
||||||
weakHandlers.clear();
|
weakHandlers.clear();
|
||||||
|
@ -1408,7 +1415,12 @@ public class ManagerImpl implements Manager {
|
||||||
|
|
||||||
dependencies.getSignalWebSocket().disconnect();
|
dependencies.getSignalWebSocket().disconnect();
|
||||||
|
|
||||||
if (closeAccount && account != null) {
|
synchronized (closedListeners) {
|
||||||
|
closedListeners.forEach(Runnable::run);
|
||||||
|
closedListeners.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (account != null) {
|
||||||
account.close();
|
account.close();
|
||||||
}
|
}
|
||||||
account = null;
|
account = null;
|
||||||
|
|
|
@ -36,6 +36,7 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||||
final String userAgent
|
final String userAgent
|
||||||
) {
|
) {
|
||||||
this.managers.addAll(managers);
|
this.managers.addAll(managers);
|
||||||
|
managers.forEach(m -> m.addClosedListener(() -> this.removeManager(m)));
|
||||||
this.dataPath = dataPath;
|
this.dataPath = dataPath;
|
||||||
this.serviceEnvironment = serviceEnvironment;
|
this.serviceEnvironment = serviceEnvironment;
|
||||||
this.userAgent = userAgent;
|
this.userAgent = userAgent;
|
||||||
|
@ -54,6 +55,7 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
managers.add(m);
|
managers.add(m);
|
||||||
|
m.addClosedListener(() -> this.removeManager(m));
|
||||||
}
|
}
|
||||||
synchronized (onManagerAddedHandlers) {
|
synchronized (onManagerAddedHandlers) {
|
||||||
for (final var handler : onManagerAddedHandlers) {
|
for (final var handler : onManagerAddedHandlers) {
|
||||||
|
@ -69,6 +71,19 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void removeManager(final Manager m) {
|
||||||
|
synchronized (managers) {
|
||||||
|
if (!managers.remove(m)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
synchronized (onManagerRemovedHandlers) {
|
||||||
|
for (final var handler : onManagerRemovedHandlers) {
|
||||||
|
handler.accept(m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addOnManagerRemovedHandler(final Consumer<Manager> handler) {
|
public void addOnManagerRemovedHandler(final Consumer<Manager> handler) {
|
||||||
synchronized (onManagerRemovedHandlers) {
|
synchronized (onManagerRemovedHandlers) {
|
||||||
|
|
|
@ -171,6 +171,10 @@ public interface Signal extends DBusInterface {
|
||||||
|
|
||||||
void submitRateLimitChallenge(String challenge, String captchaString) throws Error.Failure;
|
void submitRateLimitChallenge(String challenge, String captchaString) throws Error.Failure;
|
||||||
|
|
||||||
|
void unregister() throws Error.Failure;
|
||||||
|
|
||||||
|
void deleteAccount() throws Error.Failure;
|
||||||
|
|
||||||
class MessageReceivedV2 extends DBusSignal {
|
class MessageReceivedV2 extends DBusSignal {
|
||||||
|
|
||||||
private final long timestamp;
|
private final long timestamp;
|
||||||
|
|
|
@ -100,7 +100,7 @@ public class Main {
|
||||||
}
|
}
|
||||||
SLF4JBridgeHandler.removeHandlersForRootLogger();
|
SLF4JBridgeHandler.removeHandlersForRootLogger();
|
||||||
SLF4JBridgeHandler.install();
|
SLF4JBridgeHandler.install();
|
||||||
java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST);
|
// java.util.logging.Logger.getLogger("").setLevel(java.util.logging.Level.FINEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getStatusForError(final CommandException e) {
|
private static int getStatusForError(final CommandException e) {
|
||||||
|
|
|
@ -132,6 +132,12 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||||
runDbusSingleAccount(m, false, receiveMode != ReceiveMode.ON_START);
|
runDbusSingleAccount(m, false, receiveMode != ReceiveMode.ON_START);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m.addClosedListener(() -> {
|
||||||
|
synchronized (this) {
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
wait();
|
wait();
|
||||||
|
@ -230,7 +236,6 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runSocket(final ServerSocketChannel serverChannel, Consumer<SocketChannel> socketHandler) {
|
private void runSocket(final ServerSocketChannel serverChannel, Consumer<SocketChannel> socketHandler) {
|
||||||
final var mainThread = Thread.currentThread();
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
final SocketChannel channel;
|
final SocketChannel channel;
|
||||||
|
@ -241,7 +246,9 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||||
logger.info("Accepted new client: " + clientString);
|
logger.info("Accepted new client: " + clientString);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Failed to accept new socket connection", e);
|
logger.error("Failed to accept new socket connection", e);
|
||||||
mainThread.notifyAll();
|
synchronized (this) {
|
||||||
|
notifyAll();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
|
@ -292,6 +299,17 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
c.addOnManagerRemovedHandler(m -> {
|
||||||
|
final var path = DbusConfig.getObjectPath(m.getSelfNumber());
|
||||||
|
try {
|
||||||
|
final var object = connection.getExportedObject(null, path);
|
||||||
|
if (object instanceof DbusSignalImpl dbusSignal) {
|
||||||
|
dbusSignal.close();
|
||||||
|
}
|
||||||
|
} catch (DBusException ignored) {
|
||||||
|
}
|
||||||
|
connection.unExportObject(path);
|
||||||
|
});
|
||||||
|
|
||||||
final var initThreads = c.getAccountNumbers()
|
final var initThreads = c.getAccountNumbers()
|
||||||
.stream()
|
.stream()
|
||||||
|
|
|
@ -11,7 +11,7 @@ import org.asamk.signal.manager.Manager;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class UnregisterCommand implements LocalCommand {
|
public class UnregisterCommand implements JsonRpcLocalCommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
|
|
@ -68,6 +68,7 @@ public class DbusManagerImpl implements Manager {
|
||||||
|
|
||||||
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
|
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
|
||||||
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
|
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
|
||||||
|
private final List<Runnable> closedListeners = new ArrayList<>();
|
||||||
private DBusSigHandler<Signal.MessageReceivedV2> dbusMsgHandler;
|
private DBusSigHandler<Signal.MessageReceivedV2> dbusMsgHandler;
|
||||||
private DBusSigHandler<Signal.ReceiptReceivedV2> dbusRcptHandler;
|
private DBusSigHandler<Signal.ReceiptReceivedV2> dbusRcptHandler;
|
||||||
private DBusSigHandler<Signal.SyncMessageReceivedV2> dbusSyncHandler;
|
private DBusSigHandler<Signal.SyncMessageReceivedV2> dbusSyncHandler;
|
||||||
|
@ -583,6 +584,13 @@ public class DbusManagerImpl implements Manager {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addClosedListener(final Runnable listener) {
|
||||||
|
synchronized (closedListeners) {
|
||||||
|
closedListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
@ -595,6 +603,10 @@ public class DbusManagerImpl implements Manager {
|
||||||
weakHandlers.clear();
|
weakHandlers.clear();
|
||||||
messageHandlers.clear();
|
messageHandlers.clear();
|
||||||
}
|
}
|
||||||
|
synchronized (closedListeners) {
|
||||||
|
closedListeners.forEach(Runnable::run);
|
||||||
|
closedListeners.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SendMessageResults handleMessage(
|
private SendMessageResults handleMessage(
|
||||||
|
|
|
@ -138,6 +138,24 @@ public class DbusSignalImpl implements Signal {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unregister() throws Error.Failure {
|
||||||
|
try {
|
||||||
|
m.unregister();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new Error.Failure("Failed to unregister: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteAccount() throws Error.Failure {
|
||||||
|
try {
|
||||||
|
m.deleteAccount();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new Error.Failure("Failed to delete account: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addDevice(String uri) {
|
public void addDevice(String uri) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -64,6 +64,8 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
|
|
||||||
if (!noReceiveOnStart) {
|
if (!noReceiveOnStart) {
|
||||||
c.getAccountNumbers().stream().map(c::getManager).filter(Objects::nonNull).forEach(this::subscribeReceive);
|
c.getAccountNumbers().stream().map(c::getManager).filter(Objects::nonNull).forEach(this::subscribeReceive);
|
||||||
|
c.addOnManagerAddedHandler(this::subscribeReceive);
|
||||||
|
c.addOnManagerRemovedHandler(this::unsubscribeReceive);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleConnection();
|
handleConnection();
|
||||||
|
@ -76,6 +78,9 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
subscribeReceive(m);
|
subscribeReceive(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final var currentThread = Thread.currentThread();
|
||||||
|
m.addClosedListener(currentThread::interrupt);
|
||||||
|
|
||||||
handleConnection();
|
handleConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue