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
;
14 import java
.io
.IOException
;
15 import java
.util
.concurrent
.TimeUnit
;
17 import static org
.asamk
.signal
.DbusConfig
.SIGNAL_BUSNAME
;
18 import static org
.asamk
.signal
.DbusConfig
.SIGNAL_OBJECTPATH
;
19 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
21 public class DaemonCommand
implements LocalCommand
{
24 public void attachToSubparser(final Subparser subparser
) {
25 subparser
.addArgument("--system")
26 .action(Arguments
.storeTrue())
27 .help("Use DBus system bus instead of user bus.");
28 subparser
.addArgument("--ignore-attachments")
29 .help("Don’t download attachments of received messages.")
30 .action(Arguments
.storeTrue());
31 subparser
.addArgument("--json")
32 .help("Output received messages in json format, one json object per line.")
33 .action(Arguments
.storeTrue());
37 public int handleCommand(final Namespace ns
, final Manager m
) {
38 DBusConnection conn
= null;
41 DBusConnection
.DBusBusType busType
;
42 if (ns
.getBoolean("system")) {
43 busType
= DBusConnection
.DBusBusType
.SYSTEM
;
45 busType
= DBusConnection
.DBusBusType
.SESSION
;
47 conn
= DBusConnection
.getConnection(busType
);
48 conn
.exportObject(SIGNAL_OBJECTPATH
, new DbusSignalImpl(m
));
49 conn
.requestBusName(SIGNAL_BUSNAME
);
50 } catch (UnsatisfiedLinkError e
) {
51 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
53 } catch (DBusException e
) {
57 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
64 ?
new JsonDbusReceiveMessageHandler(m
, conn
, SIGNAL_OBJECTPATH
)
65 : new DbusReceiveMessageHandler(m
, conn
, SIGNAL_OBJECTPATH
));
67 } catch (IOException e
) {
68 System
.err
.println("Error while receiving messages: " + e
.getMessage());
70 } catch (AssertionError e
) {
71 handleAssertionError(e
);