1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.signal
.DbusConfig
;
8 import org
.asamk
.signal
.DbusReceiveMessageHandler
;
9 import org
.asamk
.signal
.JsonDbusReceiveMessageHandler
;
10 import org
.asamk
.signal
.JsonWriter
;
11 import org
.asamk
.signal
.OutputType
;
12 import org
.asamk
.signal
.OutputWriter
;
13 import org
.asamk
.signal
.PlainTextWriter
;
14 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
15 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
16 import org
.asamk
.signal
.dbus
.DbusSignalControlImpl
;
17 import org
.asamk
.signal
.dbus
.DbusSignalImpl
;
18 import org
.asamk
.signal
.manager
.Manager
;
19 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
20 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
21 import org
.slf4j
.Logger
;
22 import org
.slf4j
.LoggerFactory
;
24 import java
.io
.IOException
;
25 import java
.util
.List
;
26 import java
.util
.concurrent
.TimeUnit
;
28 public class DaemonCommand
implements MultiLocalCommand
{
30 private final static Logger logger
= LoggerFactory
.getLogger(DaemonCommand
.class);
33 public String
getName() {
38 public void attachToSubparser(final Subparser subparser
) {
39 subparser
.help("Run in daemon mode and provide an experimental dbus interface.");
40 subparser
.addArgument("--system")
41 .action(Arguments
.storeTrue())
42 .help("Use DBus system bus instead of user bus.");
43 subparser
.addArgument("--ignore-attachments")
44 .help("Don’t download attachments of received messages.")
45 .action(Arguments
.storeTrue());
49 public List
<OutputType
> getSupportedOutputTypes() {
50 return List
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
54 public void handleCommand(
55 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
56 ) throws CommandException
{
57 boolean ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
59 DBusConnection
.DBusBusType busType
;
60 if (Boolean
.TRUE
.equals(ns
.getBoolean("system"))) {
61 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
63 busType
= DBusConnection
.DBusBusType
.SESSION
;
66 try (var conn
= DBusConnection
.getConnection(busType
)) {
67 var objectPath
= DbusConfig
.getObjectPath();
68 var t
= run(conn
, objectPath
, m
, outputWriter
, ignoreAttachments
);
70 conn
.requestBusName(DbusConfig
.getBusname());
74 } catch (InterruptedException ignored
) {
76 } catch (DBusException
| IOException e
) {
77 logger
.error("Dbus command failed", e
);
78 throw new UnexpectedErrorException("Dbus command failed", e
);
83 public void handleCommand(
84 final Namespace ns
, final List
<Manager
> managers
, final SignalCreator c
, final OutputWriter outputWriter
85 ) throws CommandException
{
86 boolean ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
88 DBusConnection
.DBusBusType busType
;
89 if (Boolean
.TRUE
.equals(ns
.getBoolean("system"))) {
90 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
92 busType
= DBusConnection
.DBusBusType
.SESSION
;
95 try (var conn
= DBusConnection
.getConnection(busType
)) {
96 final var signalControl
= new DbusSignalControlImpl(c
, m
-> {
98 final var objectPath
= DbusConfig
.getObjectPath(m
.getSelfNumber());
99 return run(conn
, objectPath
, m
, outputWriter
, ignoreAttachments
);
100 } catch (DBusException e
) {
101 logger
.error("Failed to export object", e
);
104 }, DbusConfig
.getObjectPath());
105 conn
.exportObject(signalControl
);
107 for (var m
: managers
) {
108 signalControl
.addManager(m
);
111 conn
.requestBusName(DbusConfig
.getBusname());
114 } catch (DBusException
| IOException e
) {
115 logger
.error("Dbus command failed", e
);
116 throw new UnexpectedErrorException("Dbus command failed", e
);
121 DBusConnection conn
, String objectPath
, Manager m
, OutputWriter outputWriter
, boolean ignoreAttachments
122 ) throws DBusException
{
123 final var signal
= new DbusSignalImpl(m
, conn
, objectPath
);
124 conn
.exportObject(signal
);
125 final var initThread
= new Thread(signal
::initObjects
);
128 logger
.info("Exported dbus object: " + objectPath
);
130 final var thread
= new Thread(() -> {
131 while (!Thread
.interrupted()) {
133 final var receiveMessageHandler
= outputWriter
instanceof JsonWriter
134 ?
new JsonDbusReceiveMessageHandler(m
, (JsonWriter
) outputWriter
, conn
, objectPath
)
135 : new DbusReceiveMessageHandler(m
, (PlainTextWriter
) outputWriter
, conn
, objectPath
);
136 m
.receiveMessages(1, TimeUnit
.HOURS
, false, ignoreAttachments
, receiveMessageHandler
);
138 } catch (IOException e
) {
139 logger
.warn("Receiving messages failed, retrying", e
);
144 } catch (InterruptedException ignored
) {