]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/DaemonCommand.java
Move buildArgumentParser to Cli class
[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.DbusConfig;
8 import org.asamk.signal.DbusReceiveMessageHandler;
9 import org.asamk.signal.JsonDbusReceiveMessageHandler;
10 import org.asamk.signal.dbus.DbusSignalImpl;
11 import org.asamk.signal.manager.Manager;
12 import org.freedesktop.dbus.connections.impl.DBusConnection;
13 import org.freedesktop.dbus.exceptions.DBusException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 public class DaemonCommand implements MultiLocalCommand {
23
24 private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
25
26 @Override
27 public void attachToSubparser(final Subparser subparser) {
28 subparser.addArgument("--system")
29 .action(Arguments.storeTrue())
30 .help("Use DBus system bus instead of user bus.");
31 subparser.addArgument("--ignore-attachments")
32 .help("Don’t download attachments of received messages.")
33 .action(Arguments.storeTrue());
34 subparser.addArgument("--json")
35 .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.")
36 .action(Arguments.storeTrue());
37 }
38
39 @Override
40 public int handleCommand(final Namespace ns, final Manager m) {
41 boolean inJson = ns.getString("output").equals("json") || ns.getBoolean("json");
42
43 // TODO delete later when "json" variable is removed
44 if (ns.getBoolean("json")) {
45 logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
46 }
47
48 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
49
50 DBusConnection.DBusBusType busType;
51 if (ns.getBoolean("system")) {
52 busType = DBusConnection.DBusBusType.SYSTEM;
53 } else {
54 busType = DBusConnection.DBusBusType.SESSION;
55 }
56
57 try (DBusConnection conn = DBusConnection.getConnection(busType)) {
58 String objectPath = DbusConfig.getObjectPath();
59 Thread t = run(conn, objectPath, m, ignoreAttachments, inJson);
60
61 conn.requestBusName(DbusConfig.getBusname());
62
63 try {
64 t.join();
65 } catch (InterruptedException ignored) {
66 }
67 return 0;
68 } catch (DBusException | IOException e) {
69 logger.error("Dbus command failed", e);
70 return 2;
71 }
72 }
73
74 @Override
75 public int handleCommand(final Namespace ns, final List<Manager> managers) {
76 boolean inJson = ns.getString("output").equals("json") || ns.getBoolean("json");
77
78 // TODO delete later when "json" variable is removed
79 if (ns.getBoolean("json")) {
80 logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
81 }
82
83 boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
84
85 DBusConnection.DBusBusType busType;
86 if (ns.getBoolean("system")) {
87 busType = DBusConnection.DBusBusType.SYSTEM;
88 } else {
89 busType = DBusConnection.DBusBusType.SESSION;
90 }
91
92 try (DBusConnection conn = DBusConnection.getConnection(busType)) {
93 List<Thread> receiveThreads = new ArrayList<>();
94 for (Manager m : managers) {
95 String objectPath = DbusConfig.getObjectPath(m.getUsername());
96 Thread thread = run(conn, objectPath, m, ignoreAttachments, inJson);
97 receiveThreads.add(thread);
98 }
99
100 conn.requestBusName(DbusConfig.getBusname());
101
102 for (Thread t : receiveThreads) {
103 try {
104 t.join();
105 } catch (InterruptedException ignored) {
106 }
107 }
108 return 0;
109 } catch (DBusException | IOException e) {
110 logger.error("Dbus command failed", e);
111 return 2;
112 }
113 }
114
115 private Thread run(
116 DBusConnection conn, String objectPath, Manager m, boolean ignoreAttachments, boolean inJson
117 ) throws DBusException {
118 conn.exportObject(objectPath, new DbusSignalImpl(m));
119
120 final Thread thread = new Thread(() -> {
121 while (true) {
122 try {
123 m.receiveMessages(1,
124 TimeUnit.HOURS,
125 false,
126 ignoreAttachments,
127 inJson
128 ? new JsonDbusReceiveMessageHandler(m, conn, objectPath)
129 : new DbusReceiveMessageHandler(m, conn, objectPath));
130 } catch (IOException e) {
131 logger.warn("Receiving messages failed, retrying", e);
132 }
133 }
134 });
135
136 logger.info("Exported dbus object: " + objectPath);
137
138 thread.start();
139
140 return thread;
141 }
142 }