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
.OutputType
;
11 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
13 import org
.asamk
.signal
.dbus
.DbusSignalImpl
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
16 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
17 import org
.slf4j
.Logger
;
18 import org
.slf4j
.LoggerFactory
;
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import java
.util
.concurrent
.TimeUnit
;
26 public class DaemonCommand
implements MultiLocalCommand
{
28 private final static Logger logger
= LoggerFactory
.getLogger(DaemonCommand
.class);
31 public void attachToSubparser(final Subparser subparser
) {
32 subparser
.help("Run in daemon mode and provide an experimental dbus interface.");
33 subparser
.addArgument("--system")
34 .action(Arguments
.storeTrue())
35 .help("Use DBus system bus instead of user bus.");
36 subparser
.addArgument("--ignore-attachments")
37 .help("Don’t download attachments of received messages.")
38 .action(Arguments
.storeTrue());
42 public Set
<OutputType
> getSupportedOutputTypes() {
43 return Set
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
47 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
48 var inJson
= ns
.get("output") == OutputType
.JSON
;
50 boolean ignoreAttachments
= ns
.getBoolean("ignore-attachments");
52 DBusConnection
.DBusBusType busType
;
53 if (ns
.getBoolean("system")) {
54 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
56 busType
= DBusConnection
.DBusBusType
.SESSION
;
59 try (var conn
= DBusConnection
.getConnection(busType
)) {
60 var objectPath
= DbusConfig
.getObjectPath();
61 var t
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
63 conn
.requestBusName(DbusConfig
.getBusname());
67 } catch (InterruptedException ignored
) {
69 } catch (DBusException
| IOException e
) {
70 logger
.error("Dbus command failed", e
);
71 throw new UnexpectedErrorException("Dbus command failed");
76 public void handleCommand(final Namespace ns
, final List
<Manager
> managers
) throws CommandException
{
77 var inJson
= ns
.get("output") == OutputType
.JSON
;
79 boolean ignoreAttachments
= ns
.getBoolean("ignore-attachments");
81 DBusConnection
.DBusBusType busType
;
82 if (ns
.getBoolean("system")) {
83 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
85 busType
= DBusConnection
.DBusBusType
.SESSION
;
88 try (var conn
= DBusConnection
.getConnection(busType
)) {
89 var receiveThreads
= new ArrayList
<Thread
>();
90 for (var m
: managers
) {
91 var objectPath
= DbusConfig
.getObjectPath(m
.getUsername());
92 var thread
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
93 receiveThreads
.add(thread
);
96 conn
.requestBusName(DbusConfig
.getBusname());
98 for (var t
: receiveThreads
) {
101 } catch (InterruptedException ignored
) {
104 } catch (DBusException
| IOException e
) {
105 logger
.error("Dbus command failed", e
);
106 throw new UnexpectedErrorException("Dbus command failed");
111 DBusConnection conn
, String objectPath
, Manager m
, boolean ignoreAttachments
, boolean inJson
112 ) throws DBusException
{
113 conn
.exportObject(objectPath
, new DbusSignalImpl(m
));
115 final var thread
= new Thread(() -> {
123 ?
new JsonDbusReceiveMessageHandler(m
, conn
, objectPath
)
124 : new DbusReceiveMessageHandler(m
, conn
, objectPath
));
125 } catch (IOException e
) {
126 logger
.warn("Receiving messages failed, retrying", e
);
131 logger
.info("Exported dbus object: " + objectPath
);