]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/DaemonCommand.java
Implement a sticker store
[signal-cli] / src / main / java / org / asamk / signal / commands / DaemonCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.impl.Arguments;
4 import net.sourceforge.argparse4j.inf.Namespace;
5 import net.sourceforge.argparse4j.inf.Subparser;
6
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
14 import java.io.IOException;
15 import java.util.concurrent.TimeUnit;
16
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;
20
21 public class DaemonCommand implements LocalCommand {
22
23 @Override
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());
34 }
35
36 @Override
37 public int handleCommand(final Namespace ns, final Manager m) {
38 if (!m.isRegistered()) {
39 System.err.println("User is not registered.");
40 return 1;
41 }
42 DBusConnection conn = null;
43 try {
44 try {
45 DBusConnection.DBusBusType busType;
46 if (ns.getBoolean("system")) {
47 busType = DBusConnection.DBusBusType.SYSTEM;
48 } else {
49 busType = DBusConnection.DBusBusType.SESSION;
50 }
51 conn = DBusConnection.getConnection(busType);
52 conn.exportObject(SIGNAL_OBJECTPATH, new DbusSignalImpl(m));
53 conn.requestBusName(SIGNAL_BUSNAME);
54 } catch (UnsatisfiedLinkError e) {
55 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
56 return 1;
57 } catch (DBusException e) {
58 e.printStackTrace();
59 return 2;
60 }
61 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
62 try {
63 m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, ns.getBoolean("json") ? new JsonDbusReceiveMessageHandler(m, conn, SIGNAL_OBJECTPATH) : new DbusReceiveMessageHandler(m, conn, SIGNAL_OBJECTPATH));
64 return 0;
65 } catch (IOException e) {
66 System.err.println("Error while receiving messages: " + e.getMessage());
67 return 3;
68 } catch (AssertionError e) {
69 handleAssertionError(e);
70 return 1;
71 }
72 } finally {
73 if (conn != null) {
74 conn.disconnect();
75 }
76 }
77 }
78 }