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