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
.addArgument("--system")
33 .action(Arguments
.storeTrue())
34 .help("Use DBus system bus instead of user bus.");
35 subparser
.addArgument("--ignore-attachments")
36 .help("Don’t download attachments of received messages.")
37 .action(Arguments
.storeTrue());
38 subparser
.addArgument("--json")
39 .help("WARNING: This parameter is now deprecated! Please use the global \"--output=json\" option instead.\n\nOutput received messages in json format, one json object per line.")
40 .action(Arguments
.storeTrue());
44 public Set
<OutputType
> getSupportedOutputTypes() {
45 return Set
.of(OutputType
.PLAIN_TEXT
, OutputType
.JSON
);
49 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
50 var inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
52 // TODO delete later when "json" variable is removed
53 if (ns
.getBoolean("json")) {
54 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
57 boolean ignoreAttachments
= ns
.getBoolean("ignore-attachments");
59 DBusConnection
.DBusBusType busType
;
60 if (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
, ignoreAttachments
, inJson
);
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");
83 public void handleCommand(final Namespace ns
, final List
<Manager
> managers
) throws CommandException
{
84 var inJson
= ns
.get("output") == OutputType
.JSON
|| ns
.getBoolean("json");
86 // TODO delete later when "json" variable is removed
87 if (ns
.getBoolean("json")) {
88 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
91 boolean ignoreAttachments
= ns
.getBoolean("ignore-attachments");
93 DBusConnection
.DBusBusType busType
;
94 if (ns
.getBoolean("system")) {
95 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
97 busType
= DBusConnection
.DBusBusType
.SESSION
;
100 try (var conn
= DBusConnection
.getConnection(busType
)) {
101 var receiveThreads
= new ArrayList
<Thread
>();
102 for (var m
: managers
) {
103 var objectPath
= DbusConfig
.getObjectPath(m
.getUsername());
104 var thread
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
105 receiveThreads
.add(thread
);
108 conn
.requestBusName(DbusConfig
.getBusname());
110 for (var t
: receiveThreads
) {
113 } catch (InterruptedException ignored
) {
116 } catch (DBusException
| IOException e
) {
117 logger
.error("Dbus command failed", e
);
118 throw new UnexpectedErrorException("Dbus command failed");
123 DBusConnection conn
, String objectPath
, Manager m
, boolean ignoreAttachments
, boolean inJson
124 ) throws DBusException
{
125 conn
.exportObject(objectPath
, new DbusSignalImpl(m
));
127 final var thread
= new Thread(() -> {
135 ?
new JsonDbusReceiveMessageHandler(m
, conn
, objectPath
)
136 : new DbusReceiveMessageHandler(m
, conn
, objectPath
));
137 } catch (IOException e
) {
138 logger
.warn("Receiving messages failed, retrying", e
);
143 logger
.info("Exported dbus object: " + objectPath
);