]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
Update gradle wrapper
[signal-cli] / src / main / java / org / asamk / signal / Main.java
1 /*
2 Copyright (C) 2015-2018 AsamK
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package org.asamk.signal;
18
19 import net.sourceforge.argparse4j.ArgumentParsers;
20 import net.sourceforge.argparse4j.impl.Arguments;
21 import net.sourceforge.argparse4j.inf.*;
22 import org.apache.http.util.TextUtils;
23 import org.asamk.Signal;
24 import org.asamk.signal.commands.*;
25 import org.asamk.signal.manager.BaseConfig;
26 import org.asamk.signal.manager.Manager;
27 import org.asamk.signal.util.SecurityProvider;
28 import org.bouncycastle.jce.provider.BouncyCastleProvider;
29 import org.freedesktop.dbus.DBusConnection;
30 import org.freedesktop.dbus.exceptions.DBusException;
31 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
32
33 import java.io.File;
34 import java.security.Security;
35 import java.util.Map;
36
37 public class Main {
38
39 public static void main(String[] args) {
40 // Register our own security provider
41 Security.insertProviderAt(new SecurityProvider(), 1);
42 Security.addProvider(new BouncyCastleProvider());
43
44 Namespace ns = parseArgs(args);
45 if (ns == null) {
46 System.exit(1);
47 }
48
49 int res = handleCommands(ns);
50 System.exit(res);
51 }
52
53 private static int handleCommands(Namespace ns) {
54 final String username = ns.getString("username");
55 Manager m;
56 Signal ts;
57 DBusConnection dBusConn = null;
58 try {
59 if (ns.getBoolean("dbus") || ns.getBoolean("dbus_system")) {
60 try {
61 m = null;
62 int busType;
63 if (ns.getBoolean("dbus_system")) {
64 busType = DBusConnection.SYSTEM;
65 } else {
66 busType = DBusConnection.SESSION;
67 }
68 dBusConn = DBusConnection.getConnection(busType);
69 ts = dBusConn.getRemoteObject(
70 DbusConfig.SIGNAL_BUSNAME, DbusConfig.SIGNAL_OBJECTPATH,
71 Signal.class);
72 } catch (UnsatisfiedLinkError e) {
73 System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
74 return 1;
75 } catch (DBusException e) {
76 e.printStackTrace();
77 if (dBusConn != null) {
78 dBusConn.disconnect();
79 }
80 return 3;
81 }
82 } else {
83 String settingsPath = ns.getString("config");
84 if (TextUtils.isEmpty(settingsPath)) {
85 settingsPath = System.getProperty("user.home") + "/.config/signal";
86 if (!new File(settingsPath).exists()) {
87 String legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
88 if (new File(legacySettingsPath).exists()) {
89 settingsPath = legacySettingsPath;
90 }
91 }
92 }
93
94 m = new Manager(username, settingsPath);
95 ts = m;
96 try {
97 m.init();
98 } catch (Exception e) {
99 System.err.println("Error loading state file: " + e.getMessage());
100 return 2;
101 }
102 }
103
104 String commandKey = ns.getString("command");
105 final Map<String, Command> commands = Commands.getCommands();
106 if (commands.containsKey(commandKey)) {
107 Command command = commands.get(commandKey);
108
109 if (dBusConn != null) {
110 if (command instanceof ExtendedDbusCommand) {
111 return ((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn);
112 } else if (command instanceof DbusCommand) {
113 return ((DbusCommand) command).handleCommand(ns, ts);
114 } else {
115 System.err.println(commandKey + " is not yet implemented via dbus");
116 return 1;
117 }
118 } else {
119 if (command instanceof LocalCommand) {
120 return ((LocalCommand) command).handleCommand(ns, m);
121 } else if (command instanceof DbusCommand) {
122 return ((DbusCommand) command).handleCommand(ns, ts);
123 } else {
124 System.err.println(commandKey + " is only works via dbus");
125 return 1;
126 }
127 }
128 }
129 return 0;
130 } finally {
131 if (dBusConn != null) {
132 dBusConn.disconnect();
133 }
134 }
135 }
136
137 private static Namespace parseArgs(String[] args) {
138 ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
139 .build()
140 .defaultHelp(true)
141 .description("Commandline interface for Signal.")
142 .version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);
143
144 parser.addArgument("-v", "--version")
145 .help("Show package version.")
146 .action(Arguments.version());
147 parser.addArgument("--config")
148 .help("Set the path, where to store the config (Default: $HOME/.config/signal).");
149
150 MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
151 mut.addArgument("-u", "--username")
152 .help("Specify your phone number, that will be used for verification.");
153 mut.addArgument("--dbus")
154 .help("Make request via user dbus.")
155 .action(Arguments.storeTrue());
156 mut.addArgument("--dbus-system")
157 .help("Make request via system dbus.")
158 .action(Arguments.storeTrue());
159
160 Subparsers subparsers = parser.addSubparsers()
161 .title("subcommands")
162 .dest("command")
163 .description("valid subcommands")
164 .help("additional help");
165
166 final Map<String, Command> commands = Commands.getCommands();
167 for (Map.Entry<String, Command> entry : commands.entrySet()) {
168 Subparser subparser = subparsers.addParser(entry.getKey());
169 entry.getValue().attachToSubparser(subparser);
170 }
171
172 Namespace ns;
173 try {
174 ns = parser.parseArgs(args);
175 } catch (ArgumentParserException e) {
176 parser.handleError(e);
177 return null;
178 }
179
180 if ("link".equals(ns.getString("command"))) {
181 if (ns.getString("username") != null) {
182 parser.printUsage();
183 System.err.println("You cannot specify a username (phone number) when linking");
184 System.exit(2);
185 }
186 } else if (!ns.getBoolean("dbus") && !ns.getBoolean("dbus_system")) {
187 if (ns.getString("username") == null) {
188 parser.printUsage();
189 System.err.println("You need to specify a username (phone number)");
190 System.exit(2);
191 }
192 if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"))) {
193 System.err.println("Invalid username (phone number), make sure you include the country code.");
194 System.exit(2);
195 }
196 }
197 if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
198 System.err.println("You cannot specify recipients by phone number and groups a the same time");
199 System.exit(2);
200 }
201 return ns;
202 }
203 }