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
.DbusReceiveMessageHandler
;
8 import org
.asamk
.signal
.JsonDbusReceiveMessageHandler
;
9 import org
.asamk
.signal
.dbus
.DbusSignalImpl
;
10 import org
.asamk
.signal
.manager
.Manager
;
11 import org
.freedesktop
.dbus
.connections
.impl
.DBusConnection
;
12 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
13 import org
.slf4j
.Logger
;
14 import org
.slf4j
.LoggerFactory
;
16 import java
.io
.IOException
;
17 import java
.util
.concurrent
.TimeUnit
;
19 import static org
.asamk
.signal
.DbusConfig
.SIGNAL_BUSNAME
;
20 import static org
.asamk
.signal
.DbusConfig
.SIGNAL_OBJECTPATH
;
21 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
23 public class DaemonCommand
implements LocalCommand
{
25 private final static Logger logger
= LoggerFactory
.getLogger(ReceiveCommand
.class);
28 public void attachToSubparser(final Subparser subparser
) {
29 subparser
.addArgument("--system")
30 .action(Arguments
.storeTrue())
31 .help("Use DBus system bus instead of user bus.");
32 subparser
.addArgument("--ignore-attachments")
33 .help("Don’t download attachments of received messages.")
34 .action(Arguments
.storeTrue());
35 subparser
.addArgument("--json")
36 .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.")
37 .action(Arguments
.storeTrue());
41 public int handleCommand(final Namespace ns
, final Manager m
) {
42 boolean inJson
= ns
.getString("output").equals("json") || ns
.getBoolean("json");
44 // TODO delete later when "json" variable is removed
45 if (ns
.getBoolean("json")) {
46 logger
.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
49 DBusConnection conn
= null;
52 DBusConnection
.DBusBusType busType
;
53 if (ns
.getBoolean("system")) {
54 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
56 busType
= DBusConnection
.DBusBusType
.SESSION
;
58 conn
= DBusConnection
.getConnection(busType
);
59 conn
.exportObject(SIGNAL_OBJECTPATH
, new DbusSignalImpl(m
));
60 conn
.requestBusName(SIGNAL_BUSNAME
);
61 } catch (UnsatisfiedLinkError e
) {
62 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
64 } catch (DBusException e
) {
68 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
75 ?
new JsonDbusReceiveMessageHandler(m
, conn
, SIGNAL_OBJECTPATH
)
76 : new DbusReceiveMessageHandler(m
, conn
, SIGNAL_OBJECTPATH
));
78 } catch (IOException e
) {
79 System
.err
.println("Error while receiving messages: " + e
.getMessage());
81 } catch (AssertionError e
) {
82 handleAssertionError(e
);