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
.freedesktop
.dbus
.DBusConnection
;
28 import org
.freedesktop
.dbus
.exceptions
.DBusException
;
29 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
32 import java
.security
.Security
;
37 public static void main(String
[] args
) {
38 // Workaround for BKS truststore
39 Security
.insertProviderAt(new org
.bouncycastle
.jce
.provider
.BouncyCastleProvider(), 1);
41 Namespace ns
= parseArgs(args
);
46 int res
= handleCommands(ns
);
50 private static int handleCommands(Namespace ns
) {
51 final String username
= ns
.getString("username");
54 DBusConnection dBusConn
= null;
56 if (ns
.getBoolean("dbus") || ns
.getBoolean("dbus_system")) {
60 if (ns
.getBoolean("dbus_system")) {
61 busType
= DBusConnection
.SYSTEM
;
63 busType
= DBusConnection
.SESSION
;
65 dBusConn
= DBusConnection
.getConnection(busType
);
66 ts
= dBusConn
.getRemoteObject(
67 DbusConfig
.SIGNAL_BUSNAME
, DbusConfig
.SIGNAL_OBJECTPATH
,
69 } catch (UnsatisfiedLinkError e
) {
70 System
.err
.println("Missing native library dependency for dbus service: " + e
.getMessage());
72 } catch (DBusException e
) {
74 if (dBusConn
!= null) {
75 dBusConn
.disconnect();
80 String settingsPath
= ns
.getString("config");
81 if (TextUtils
.isEmpty(settingsPath
)) {
82 settingsPath
= System
.getProperty("user.home") + "/.config/signal";
83 if (!new File(settingsPath
).exists()) {
84 String legacySettingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
85 if (new File(legacySettingsPath
).exists()) {
86 settingsPath
= legacySettingsPath
;
91 m
= new Manager(username
, settingsPath
);
95 } catch (Exception e
) {
96 System
.err
.println("Error loading state file: " + e
.getMessage());
101 String commandKey
= ns
.getString("command");
102 final Map
<String
, Command
> commands
= Commands
.getCommands();
103 if (commands
.containsKey(commandKey
)) {
104 Command command
= commands
.get(commandKey
);
106 if (dBusConn
!= null) {
107 if (command
instanceof ExtendedDbusCommand
) {
108 return ((ExtendedDbusCommand
) command
).handleCommand(ns
, ts
, dBusConn
);
109 } else if (command
instanceof DbusCommand
) {
110 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
112 System
.err
.println(commandKey
+ " is not yet implemented via dbus");
116 if (command
instanceof LocalCommand
) {
117 return ((LocalCommand
) command
).handleCommand(ns
, m
);
118 } else if (command
instanceof DbusCommand
) {
119 return ((DbusCommand
) command
).handleCommand(ns
, ts
);
121 System
.err
.println(commandKey
+ " is only works via dbus");
128 if (dBusConn
!= null) {
129 dBusConn
.disconnect();
134 private static Namespace
parseArgs(String
[] args
) {
135 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
138 .description("Commandline interface for Signal.")
139 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
141 parser
.addArgument("-v", "--version")
142 .help("Show package version.")
143 .action(Arguments
.version());
144 parser
.addArgument("--config")
145 .help("Set the path, where to store the config (Default: $HOME/.config/signal).");
147 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
148 mut
.addArgument("-u", "--username")
149 .help("Specify your phone number, that will be used for verification.");
150 mut
.addArgument("--dbus")
151 .help("Make request via user dbus.")
152 .action(Arguments
.storeTrue());
153 mut
.addArgument("--dbus-system")
154 .help("Make request via system dbus.")
155 .action(Arguments
.storeTrue());
157 Subparsers subparsers
= parser
.addSubparsers()
158 .title("subcommands")
160 .description("valid subcommands")
161 .help("additional help");
163 final Map
<String
, Command
> commands
= Commands
.getCommands();
164 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
165 Subparser subparser
= subparsers
.addParser(entry
.getKey());
166 entry
.getValue().attachToSubparser(subparser
);
171 ns
= parser
.parseArgs(args
);
172 } catch (ArgumentParserException e
) {
173 parser
.handleError(e
);
177 if ("link".equals(ns
.getString("command"))) {
178 if (ns
.getString("username") != null) {
180 System
.err
.println("You cannot specify a username (phone number) when linking");
183 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
184 if (ns
.getString("username") == null) {
186 System
.err
.println("You need to specify a username (phone number)");
189 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"))) {
190 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
194 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
195 System
.err
.println("You cannot specify recipients by phone number and groups a the same time");