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
.dbus
.DbusSignalImpl
;
11 import org
.asamk
.signal
.manager
.Manager
;
12 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
13 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
14 import org
.slf4j
.Logger
;
15 import org
.slf4j
.LoggerFactory
;
17 import java
.io
.IOException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.TimeUnit
;
22 public class DaemonCommand
implements MultiLocalCommand
{
24 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
27 public void attachToSubparser(final Subparser subparser
) {
28 subparser
.addArgument("--system")
29 .action(Arguments
.storeTrue())
30 .help("Use DBus system bus instead of user bus.");
31 subparser
.addArgument("--ignore-attachments")
32 .help("Don’t download attachments of received messages.")
33 .action(Arguments
.storeTrue());
34 subparser
.addArgument("--json")
35 .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.")
36 .action(Arguments
.storeTrue());
40 public int handleCommand(final Namespace ns
, final Manager m
) {
41 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
43 // TODO delete later when "json" variable is removed
44 if (ns
.getBoolean("json")) {
45 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
48 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
50 DBusConnection
.DBusBusType busType
;
51 if (ns
.getBoolean("system")) {
52 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
54 busType
= DBusConnection
.DBusBusType
.SESSION
;
57 try (DBusConnection conn
= DBusConnection
.getConnection(busType
)) {
58 String objectPath
= DbusConfig
.getObjectPath();
59 Thread t
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
61 conn
.requestBusName(DbusConfig
.getBusname());
65 } catch (InterruptedException ignored
) {
68 } catch (DBusException
| IOException e
) {
69 logger
.error("Dbus command failed", e
);
75 public int handleCommand(final Namespace ns
, final List
<Manager
> managers
) {
76 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
78 // TODO delete later when "json" variable is removed
79 if (ns
.getBoolean("json")) {
80 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
83 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
85 DBusConnection
.DBusBusType busType
;
86 if (ns
.getBoolean("system")) {
87 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
89 busType
= DBusConnection
.DBusBusType
.SESSION
;
92 try (DBusConnection conn
= DBusConnection
.getConnection(busType
)) {
93 List
<Thread
> receiveThreads
= new ArrayList
<>();
94 for (Manager m
: managers
) {
95 String objectPath
= DbusConfig
.getObjectPath(m
.getUsername());
96 Thread thread
= run(conn
, objectPath
, m
, ignoreAttachments
, inJson
);
97 receiveThreads
.add(thread
);
100 conn
.requestBusName(DbusConfig
.getBusname());
102 for (Thread t
: receiveThreads
) {
105 } catch (InterruptedException ignored
) {
109 } catch (DBusException
| IOException e
) {
110 logger
.error("Dbus command failed", e
);
116 DBusConnection conn
, String objectPath
, Manager m
, boolean ignoreAttachments
, boolean inJson
117 ) throws DBusException
{
118 conn
.exportObject(objectPath
, new DbusSignalImpl(m
));
120 final Thread thread
= new Thread(() -> {
128 ?
new JsonDbusReceiveMessageHandler(m
, conn
, objectPath
)
129 : new DbusReceiveMessageHandler(m
, conn
, objectPath
));
130 } catch (IOException e
) {
131 logger
.warn("Receiving messages failed, retrying", e
);
136 logger
.info("Exported dbus object: " + objectPath
);