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
;
6 import org
.asamk
.signal
.DbusReceiveMessageHandler
;
7 import org
.asamk
.signal
.JsonDbusReceiveMessageHandler
;
8 import org
.asamk
.signal
.manager
.Manager
;
9 import org
.freedesktop
.dbus
.DBusConnection
;
10 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
12 import java
.io
.IOException
;
13 import java
.util
.concurrent
.TimeUnit
;
15 import static org
.asamk
.signal
.DbusConfig
.SIGNAL_BUSNAME
;
16 import static org
.asamk
.signal
.DbusConfig
.SIGNAL_OBJECTPATH
;
17 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
19 public class DaemonCommand
implements LocalCommand
{
22 public void attachToSubparser(final Subparser subparser
) {
23 subparser
.addArgument("--system")
24 .action(Arguments
.storeTrue())
25 .help("Use DBus system bus instead of user bus.");
26 subparser
.addArgument("--ignore-attachments")
27 .help("Don’t download attachments of received messages.")
28 .action(Arguments
.storeTrue());
29 subparser
.addArgument("--json")
30 .help("Output received messages in json format, one json object per line.")
31 .action(Arguments
.storeTrue());
35 public int handleCommand(final Namespace ns
, final Manager m
) {
36 if (!m
.isRegistered()) {
37 System
.err
.println("User is not registered.");
40 DBusConnection conn
= null;
44 if (ns
.getBoolean("system")) {
45 busType
= DBusConnection
.SYSTEM
;
47 busType
= DBusConnection
.SESSION
;
49 conn
= DBusConnection
.getConnection(busType
);
50 conn
.exportObject(SIGNAL_OBJECTPATH
, m
);
51 conn
.requestBusName(SIGNAL_BUSNAME
);
52 } catch (UnsatisfiedLinkError e
) {
53 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
55 } catch (DBusException e
) {
59 boolean ignoreAttachments
= ns
.getBoolean("ignore_attachments");
61 m
.receiveMessages(1, TimeUnit
.HOURS
, false, ignoreAttachments
, ns
.getBoolean("json") ?
new JsonDbusReceiveMessageHandler(m
, conn
, SIGNAL_OBJECTPATH
) : new DbusReceiveMessageHandler(m
, conn
, SIGNAL_OBJECTPATH
));
63 } catch (IOException e
) {
64 System
.err
.println("Error while receiving messages: " + e
.getMessage());
66 } catch (AssertionError e
) {
67 handleAssertionError(e
);