2 Copyright (C) 2015-2018 AsamK
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
.*;
22 import org
.asamk
.Signal
;
23 import org
.asamk
.signal
.commands
.*;
24 import org
.asamk
.signal
.manager
.BaseConfig
;
25 import org
.asamk
.signal
.manager
.Manager
;
26 import org
.asamk
.signal
.util
.IOUtils
;
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
;
34 import java
.security
.Security
;
37 import static org
.whispersystems
.signalservice
.internal
.util
.Util
.isEmpty
;
41 public static void main(String
[] args
) {
42 // Register our own security provider
43 Security
.insertProviderAt(new SecurityProvider(), 1);
44 Security
.addProvider(new BouncyCastleProvider());
46 Namespace ns
= parseArgs(args
);
51 int res
= handleCommands(ns
);
55 private static int handleCommands(Namespace ns
) {
56 final String username
= ns
.getString("username");
59 DBusConnection dBusConn
= null;
61 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
65 if (ns
.getBoolean("dbus_system")) {
66 busType
= DBusConnection
.SYSTEM
;
68 busType
= DBusConnection
.SESSION
;
70 dBusConn
= DBusConnection
.getConnection(busType
);
71 ts
= dBusConn
.getRemoteObject(
72 DbusConfig
.SIGNAL_BUSNAME
, DbusConfig
.SIGNAL_OBJECTPATH
,
74 } catch (UnsatisfiedLinkError e
) {
75 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
77 } catch (DBusException e
) {
79 if (dBusConn
!= null) {
80 dBusConn
.disconnect();
85 String dataPath
= ns
.getString("config");
86 if (isEmpty(dataPath
)) {
87 dataPath
= getDefaultDataPath();
90 m
= new Manager(username
, dataPath
);
94 } catch (Exception e
) {
95 System
.err
.println("Error loading state file: " + e
.getMessage());
100 String commandKey
= ns
.getString("command");
101 final Map
<String
, Command
> commands
= Commands
.getCommands();
102 if (commands
.containsKey(commandKey
)) {
103 Command command
= commands
.get(commandKey
);
105 if (dBusConn
!= null) {
106 if (command
instanceof ExtendedDbusCommand
) {
107 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
108 } else if (command
instanceof DbusCommand
) {
109 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
111 System
.err
.println(commandKey
+ " is not yet implemented via dbus");
115 if (command
instanceof LocalCommand
) {
116 return ((LocalCommand
) command
).handleCommand(ns
, m
);
117 } else if (command
instanceof DbusCommand
) {
118 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
120 System
.err
.println(commandKey
+ " is only works via dbus");
127 if (dBusConn
!= null) {
128 dBusConn
.disconnect();
134 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
135 * - $HOME/.config/signal
136 * - $HOME/.config/textsecure
138 * @return the data directory to be used by signal-cli.
140 private static String
getDefaultDataPath() {
141 String dataPath
= IOUtils
.getDataHomeDir() + "/signal-cli";
142 if (new File(dataPath
).exists()) {
146 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/signal";
147 if (new File(legacySettingsPath
).exists()) {
148 return legacySettingsPath
;
151 legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
152 if (new File(legacySettingsPath
).exists()) {
153 return legacySettingsPath
;
159 private static Namespace
parseArgs(String
[] args
) {
160 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
163 .description("Commandline interface for Signal.")
164 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
166 parser
.addArgument("-v", "--version")
167 .help("Show package version.")
168 .action(Arguments
.version());
169 parser
.addArgument("--config")
170 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
172 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
173 mut
.addArgument("-u", "--username")
174 .help("Specify your phone number, that will be used for verification.");
175 mut
.addArgument("--dbus")
176 .help("Make request via user dbus.")
177 .action(Arguments
.storeTrue());
178 mut
.addArgument("--dbus-system")
179 .help("Make request via system dbus.")
180 .action(Arguments
.storeTrue());
182 Subparsers subparsers
= parser
.addSubparsers()
183 .title("subcommands")
185 .description("valid subcommands")
186 .help("additional help");
188 final Map
<String
, Command
> commands
= Commands
.getCommands();
189 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
190 Subparser subparser
= subparsers
.addParser(entry
.getKey());
191 entry
.getValue().attachToSubparser(subparser
);
196 ns
= parser
.parseArgs(args
);
197 } catch (ArgumentParserException e
) {
198 parser
.handleError(e
);
202 if ("link".equals(ns
.getString("command"))) {
203 if (ns
.getString("username") != null) {
205 System
.err
.println("You cannot specify a username (phone number) when linking");
208 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
209 if (ns
.getString("username") == null) {
211 System
.err
.println("You need to specify a username (phone number)");
214 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"), null)) {
215 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
219 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
220 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");