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