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
.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
.IOUtils
;
28 import org
.asamk
.signal
.util
.SecurityProvider
;
29 import org
.bouncycastle
.jce
.provider
.BouncyCastleProvider
;
30 import org
.freedesktop
.dbus
.DBusConnection
;
31 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
32 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
35 import java
.security
.Security
;
40 public static void main(String
[] args
) {
41 // Register our own security provider
42 Security
.insertProviderAt(new SecurityProvider(), 1);
43 Security
.addProvider(new BouncyCastleProvider());
45 Namespace ns
= parseArgs(args
);
50 int res
= handleCommands(ns
);
54 private static int handleCommands(Namespace ns
) {
55 final String username
= ns
.getString("username");
58 DBusConnection dBusConn
= null;
60 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
64 if (ns
.getBoolean("dbus_system")) {
65 busType
= DBusConnection
.SYSTEM
;
67 busType
= DBusConnection
.SESSION
;
69 dBusConn
= DBusConnection
.getConnection(busType
);
70 ts
= dBusConn
.getRemoteObject(
71 DbusConfig
.SIGNAL_BUSNAME
, DbusConfig
.SIGNAL_OBJECTPATH
,
73 } catch (UnsatisfiedLinkError e
) {
74 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
76 } catch (DBusException e
) {
78 if (dBusConn
!= null) {
79 dBusConn
.disconnect();
84 String dataPath
= ns
.getString("config");
85 if (TextUtils
.isEmpty(dataPath
)) {
86 dataPath
= getDefaultDataPath();
89 m
= new Manager(username
, dataPath
);
93 } catch (Exception e
) {
94 System
.err
.println("Error loading state file: " + e
.getMessage());
99 String commandKey
= ns
.getString("command");
100 final Map
<String
, Command
> commands
= Commands
.getCommands();
101 if (commands
.containsKey(commandKey
)) {
102 Command command
= commands
.get(commandKey
);
104 if (dBusConn
!= null) {
105 if (command
instanceof ExtendedDbusCommand
) {
106 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
107 } else if (command
instanceof DbusCommand
) {
108 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
110 System
.err
.println(commandKey
+ " is not yet implemented via dbus");
114 if (command
instanceof LocalCommand
) {
115 return ((LocalCommand
) command
).handleCommand(ns
, m
);
116 } else if (command
instanceof DbusCommand
) {
117 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
119 System
.err
.println(commandKey
+ " is only works via dbus");
126 if (dBusConn
!= null) {
127 dBusConn
.disconnect();
133 * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
134 * - $HOME/.config/signal
135 * - $HOME/.config/textsecure
137 * @return the data directory to be used by signal-cli.
139 private static String
getDefaultDataPath() {
140 String dataPath
= IOUtils
.getDataHomeDir() + "/signal-cli";
141 if (new File(dataPath
).exists()) {
145 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/signal";
146 if (new File(legacySettingsPath
).exists()) {
147 return legacySettingsPath
;
150 legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
151 if (new File(legacySettingsPath
).exists()) {
152 return legacySettingsPath
;
158 private static Namespace
parseArgs(String
[] args
) {
159 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
162 .description("Commandline interface for Signal.")
163 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
165 parser
.addArgument("-v", "--version")
166 .help("Show package version.")
167 .action(Arguments
.version());
168 parser
.addArgument("--config")
169 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
171 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
172 mut
.addArgument("-u", "--username")
173 .help("Specify your phone number, that will be used for verification.");
174 mut
.addArgument("--dbus")
175 .help("Make request via user dbus.")
176 .action(Arguments
.storeTrue());
177 mut
.addArgument("--dbus-system")
178 .help("Make request via system dbus.")
179 .action(Arguments
.storeTrue());
181 Subparsers subparsers
= parser
.addSubparsers()
182 .title("subcommands")
184 .description("valid subcommands")
185 .help("additional help");
187 final Map
<String
, Command
> commands
= Commands
.getCommands();
188 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
189 Subparser subparser
= subparsers
.addParser(entry
.getKey());
190 entry
.getValue().attachToSubparser(subparser
);
195 ns
= parser
.parseArgs(args
);
196 } catch (ArgumentParserException e
) {
197 parser
.handleError(e
);
201 if ("link".equals(ns
.getString("command"))) {
202 if (ns
.getString("username") != null) {
204 System
.err
.println("You cannot specify a username (phone number) when linking");
207 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
208 if (ns
.getString("username") == null) {
210 System
.err
.println("You need to specify a username (phone number)");
213 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"), null)) {
214 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
218 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
219 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");