mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Closes #759 commit169bb518bf
Author: John Freed <okgithub@johnfreed.com> Date: Fri Oct 15 08:53:34 2021 +0200 remove org.asamk.signal.manager.api.Configuration commit27ecfee382
Author: John Freed <okgithub@johnfreed.com> Date: Fri Oct 15 08:40:13 2021 +0200 add a DBus "daemon running" indicator commit94b3d9e2ed
Author: John Freed <okgithub@johnfreed.com> Date: Fri Oct 15 08:20:03 2021 +0200 changing most DBus logger.info to logger.debug commit15da060127
Author: John Freed <okgithub@johnfreed.com> Date: Fri Oct 15 00:09:26 2021 +0200 Configuration for Dbus and main Main program subcommand - fix logic to take into account previously unset flags - provide output in json and plain-text formats new Dbus Properties: - ConfigurationReadReceipts - ConfigurationUnidentifiedDeliveryIndicators - ConfigurationTypingIndicators - ConfigurationLinkPreviews removed getConfiguration and setConfiguration methods updated documentation commit09da3aae62
Merge:3c40b11
18ad9fb
Author: John Freed <okgithub@johnfreed.com> Date: Thu Oct 14 15:24:07 2021 +0200 Merge branch master into dbus_updateConfiguration commit3c40b11b8a
Merge:8416d4a
cadcc6c
Author: John Freed <okgithub@johnfreed.com> Date: Sat Oct 9 14:08:08 2021 +0200 Merge branch master into dbus_updateConfiguration commit8416d4ac47
Author: John Freed <okgithub@johnfreed.com> Date: Mon Oct 4 08:48:56 2021 +0200 Dbus get/setConfiguration methods implement: - getConfiguration() -> [readReceipts<b>, unidentifiedDeliveryIndicators<b>, typingIndicators<b>, linkPreviews<b>] -> <>:: - setConfiguration(readReceipts<b>, unidentifiedDeliveryIndicators<b>, typingIndicators<b>, linkPreviews<b>) -> <>:: Update documentation
146 lines
5.5 KiB
Java
146 lines
5.5 KiB
Java
package org.asamk.signal.commands;
|
||
|
||
import net.sourceforge.argparse4j.impl.Arguments;
|
||
import net.sourceforge.argparse4j.inf.Namespace;
|
||
import net.sourceforge.argparse4j.inf.Subparser;
|
||
|
||
import org.asamk.signal.DbusConfig;
|
||
import org.asamk.signal.DbusReceiveMessageHandler;
|
||
import org.asamk.signal.JsonReceiveMessageHandler;
|
||
import org.asamk.signal.JsonWriter;
|
||
import org.asamk.signal.OutputType;
|
||
import org.asamk.signal.OutputWriter;
|
||
import org.asamk.signal.PlainTextWriter;
|
||
import org.asamk.signal.ReceiveMessageHandler;
|
||
import org.asamk.signal.commands.exceptions.CommandException;
|
||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||
import org.asamk.signal.dbus.DbusSignalControlImpl;
|
||
import org.asamk.signal.dbus.DbusSignalImpl;
|
||
import org.asamk.signal.manager.Manager;
|
||
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||
import org.freedesktop.dbus.exceptions.DBusException;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import java.io.IOException;
|
||
import java.util.List;
|
||
|
||
public class DaemonCommand implements MultiLocalCommand {
|
||
|
||
private final static Logger logger = LoggerFactory.getLogger(DaemonCommand.class);
|
||
|
||
@Override
|
||
public String getName() {
|
||
return "daemon";
|
||
}
|
||
|
||
@Override
|
||
public void attachToSubparser(final Subparser subparser) {
|
||
subparser.help("Run in daemon mode and provide an experimental dbus interface.");
|
||
subparser.addArgument("--system")
|
||
.action(Arguments.storeTrue())
|
||
.help("Use DBus system bus instead of user bus.");
|
||
subparser.addArgument("--ignore-attachments")
|
||
.help("Don’t download attachments of received messages.")
|
||
.action(Arguments.storeTrue());
|
||
}
|
||
|
||
@Override
|
||
public List<OutputType> getSupportedOutputTypes() {
|
||
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
|
||
}
|
||
|
||
@Override
|
||
public void handleCommand(
|
||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||
) throws CommandException {
|
||
boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
|
||
m.setIgnoreAttachments(ignoreAttachments);
|
||
|
||
DBusConnection.DBusBusType busType;
|
||
if (Boolean.TRUE.equals(ns.getBoolean("system"))) {
|
||
busType = DBusConnection.DBusBusType.SYSTEM;
|
||
} else {
|
||
busType = DBusConnection.DBusBusType.SESSION;
|
||
}
|
||
|
||
try (var conn = DBusConnection.getConnection(busType)) {
|
||
var objectPath = DbusConfig.getObjectPath();
|
||
var t = run(conn, objectPath, m, outputWriter);
|
||
|
||
conn.requestBusName(DbusConfig.getBusname());
|
||
logger.info("DBus daemon running in single-user mode for " + m.getSelfNumber());
|
||
|
||
try {
|
||
t.join();
|
||
synchronized (this) {
|
||
wait();
|
||
}
|
||
} catch (InterruptedException ignored) {
|
||
}
|
||
} catch (DBusException | IOException e) {
|
||
logger.error("Dbus command failed", e);
|
||
throw new UnexpectedErrorException("Dbus command failed", e);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void handleCommand(
|
||
final Namespace ns, final List<Manager> managers, final SignalCreator c, final OutputWriter outputWriter
|
||
) throws CommandException {
|
||
boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
|
||
|
||
DBusConnection.DBusBusType busType;
|
||
if (Boolean.TRUE.equals(ns.getBoolean("system"))) {
|
||
busType = DBusConnection.DBusBusType.SYSTEM;
|
||
} else {
|
||
busType = DBusConnection.DBusBusType.SESSION;
|
||
}
|
||
|
||
try (var conn = DBusConnection.getConnection(busType)) {
|
||
final var signalControl = new DbusSignalControlImpl(c, m -> {
|
||
m.setIgnoreAttachments(ignoreAttachments);
|
||
try {
|
||
final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
|
||
return run(conn, objectPath, m, outputWriter);
|
||
} catch (DBusException e) {
|
||
logger.error("Failed to export object", e);
|
||
return null;
|
||
}
|
||
}, DbusConfig.getObjectPath());
|
||
conn.exportObject(signalControl);
|
||
|
||
for (var m : managers) {
|
||
signalControl.addManager(m);
|
||
}
|
||
|
||
conn.requestBusName(DbusConfig.getBusname());
|
||
logger.info("DBus daemon running in mulit-account mode");
|
||
|
||
signalControl.run();
|
||
} catch (DBusException | IOException e) {
|
||
logger.error("Dbus command failed", e);
|
||
throw new UnexpectedErrorException("Dbus command failed", e);
|
||
}
|
||
}
|
||
|
||
private Thread run(
|
||
DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter
|
||
) throws DBusException {
|
||
final var signal = new DbusSignalImpl(m, conn, objectPath);
|
||
conn.exportObject(signal);
|
||
final var initThread = new Thread(signal::initObjects);
|
||
initThread.start();
|
||
|
||
logger.debug("Exported dbus object: " + objectPath);
|
||
|
||
final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m,
|
||
(JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter);
|
||
m.addReceiveHandler(handler);
|
||
|
||
final var dbusMessageHandler = new DbusReceiveMessageHandler(m, conn, objectPath);
|
||
m.addReceiveHandler(dbusMessageHandler);
|
||
|
||
return initThread;
|
||
}
|
||
}
|