2 Copyright (C) 2015-2020 AsamK and contributors
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.
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.
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/>.
17 package org
.asamk
.signal
;
19 import net
.sourceforge
.argparse4j
.ArgumentParsers
;
20 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
21 import net
.sourceforge
.argparse4j
.inf
.ArgumentParser
;
22 import net
.sourceforge
.argparse4j
.inf
.ArgumentParserException
;
23 import net
.sourceforge
.argparse4j
.inf
.MutuallyExclusiveGroup
;
24 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
25 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
26 import net
.sourceforge
.argparse4j
.inf
.Subparsers
;
28 import org
.asamk
.Signal
;
29 import org
.asamk
.signal
.commands
.Command
;
30 import org
.asamk
.signal
.commands
.Commands
;
31 import org
.asamk
.signal
.commands
.DbusCommand
;
32 import org
.asamk
.signal
.commands
.ExtendedDbusCommand
;
33 import org
.asamk
.signal
.commands
.LocalCommand
;
34 import org
.asamk
.signal
.manager
.BaseConfig
;
35 import org
.asamk
.signal
.manager
.Manager
;
36 import org
.asamk
.signal
.util
.IOUtils
;
37 import org
.asamk
.signal
.util
.SecurityProvider
;
38 import org
.bouncycastle
.jce
.provider
.BouncyCastleProvider
;
39 import org
.freedesktop
.dbus
.DBusConnection
;
40 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
41 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
42 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
45 import java
.security
.Security
;
48 import static org
.whispersystems
.signalservice
.internal
.util
.Util
.isEmpty
;
52 public static void main(String
[] args
) {
53 installSecurityProviderWorkaround();
55 Namespace ns
= parseArgs(args
);
60 int res
= handleCommands(ns
);
64 public static void installSecurityProviderWorkaround() {
65 // Register our own security provider
66 Security
.insertProviderAt(new SecurityProvider(), 1);
67 Security
.addProvider(new BouncyCastleProvider());
70 private static int handleCommands(Namespace ns
) {
71 final String username
= ns
.getString("username");
74 DBusConnection dBusConn
= null;
76 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
80 if (ns
.getBoolean("dbus_system")) {
81 busType
= DBusConnection
.SYSTEM
;
83 busType
= DBusConnection
.SESSION
;
85 dBusConn
= DBusConnection
.getConnection(busType
);
86 ts
= dBusConn
.getRemoteObject(
87 DbusConfig
.SIGNAL_BUSNAME
, DbusConfig
.SIGNAL_OBJECTPATH
,
89 } catch (UnsatisfiedLinkError e
) {
90 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
92 } catch (DBusException e
) {
94 if (dBusConn
!= null) {
95 dBusConn
.disconnect();
100 String dataPath
= ns
.getString("config");
101 if (isEmpty(dataPath
)) {
102 dataPath
= getDefaultDataPath();
105 m
= new Manager(username
, dataPath
);
109 } catch (AuthorizationFailedException e
) {
110 if (!"register".equals(ns
.getString("command"))) {
111 // Register command should still be possible, if current authorization fails
112 System
.err
.println("Authorization failed, was the number registered elsewhere?");
115 } catch (Exception e
) {
116 System
.err
.println("Error loading state file: " + e
.getMessage());
121 String commandKey
= ns
.getString("command");
122 final Map
<String
, Command
> commands
= Commands
.getCommands();
123 if (commands
.containsKey(commandKey
)) {
124 Command command
= commands
.get(commandKey
);
126 if (dBusConn
!= null) {
127 if (command
instanceof ExtendedDbusCommand
) {
128 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
129 } else if (command
instanceof DbusCommand
) {
130 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
132 System
.err
.println(commandKey
+ " is not yet implemented via dbus");
136 if (command
instanceof LocalCommand
) {
137 return ((LocalCommand
) command
).handleCommand(ns
, m
);
138 } else if (command
instanceof DbusCommand
) {
139 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
141 System
.err
.println(commandKey
+ " is only works via dbus");
148 if (dBusConn
!= null) {
149 dBusConn
.disconnect();
155 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
156 * - $HOME/.config/signal
157 * - $HOME/.config/textsecure
159 * @return the data directory to be used by signal-cli.
161 private static String
getDefaultDataPath() {
162 String dataPath
= IOUtils
.getDataHomeDir() + "/signal-cli";
163 if (new File(dataPath
).exists()) {
167 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/signal";
168 if (new File(legacySettingsPath
).exists()) {
169 return legacySettingsPath
;
172 legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
173 if (new File(legacySettingsPath
).exists()) {
174 return legacySettingsPath
;
180 private static Namespace
parseArgs(String
[] args
) {
181 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
184 .description("Commandline interface for Signal.")
185 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
187 parser
.addArgument("-v", "--version")
188 .help("Show package version.")
189 .action(Arguments
.version());
190 parser
.addArgument("--config")
191 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
193 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
194 mut
.addArgument("-u", "--username")
195 .help("Specify your phone number, that will be used for verification.");
196 mut
.addArgument("--dbus")
197 .help("Make request via user dbus.")
198 .action(Arguments
.storeTrue());
199 mut
.addArgument("--dbus-system")
200 .help("Make request via system dbus.")
201 .action(Arguments
.storeTrue());
203 Subparsers subparsers
= parser
.addSubparsers()
204 .title("subcommands")
206 .description("valid subcommands")
207 .help("additional help");
209 final Map
<String
, Command
> commands
= Commands
.getCommands();
210 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
211 Subparser subparser
= subparsers
.addParser(entry
.getKey());
212 entry
.getValue().attachToSubparser(subparser
);
217 ns
= parser
.parseArgs(args
);
218 } catch (ArgumentParserException e
) {
219 parser
.handleError(e
);
223 if ("link".equals(ns
.getString("command"))) {
224 if (ns
.getString("username") != null) {
226 System
.err
.println("You cannot specify a username (phone number) when linking");
229 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
230 if (ns
.getString("username") == null) {
232 System
.err
.println("You need to specify a username (phone number)");
235 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"), null)) {
236 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
240 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
241 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");