2 Copyright (C) 2015-2021 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
.commands
.Command
;
29 import org
.asamk
.signal
.commands
.Commands
;
30 import org
.asamk
.signal
.manager
.LibSignalLogger
;
31 import org
.asamk
.signal
.util
.SecurityProvider
;
32 import org
.bouncycastle
.jce
.provider
.BouncyCastleProvider
;
33 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
35 import java
.security
.Security
;
40 public static void main(String
[] args
) {
41 installSecurityProviderWorkaround();
43 // Configuring the logger needs to happen before any logger is initialized
44 if (isVerbose(args
)) {
45 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
46 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
47 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
48 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
49 System
.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
50 LibSignalLogger
.initLogger();
52 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
53 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
54 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
55 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
58 Namespace ns
= parseArgs(args
);
63 int res
= new Cli(ns
).init();
67 private static void installSecurityProviderWorkaround() {
68 // Register our own security provider
69 Security
.insertProviderAt(new SecurityProvider(), 1);
70 Security
.addProvider(new BouncyCastleProvider());
73 private static boolean isVerbose(String
[] args
) {
74 ArgumentParser parser
= buildBaseArgumentParser();
78 ns
= parser
.parseKnownArgs(args
, null);
79 } catch (ArgumentParserException e
) {
83 return ns
.getBoolean("verbose");
86 private static Namespace
parseArgs(String
[] args
) {
87 ArgumentParser parser
= buildArgumentParser();
91 ns
= parser
.parseArgs(args
);
92 } catch (ArgumentParserException e
) {
93 parser
.handleError(e
);
97 if ("link".equals(ns
.getString("command"))) {
98 if (ns
.getString("username") != null) {
100 System
.err
.println("You cannot specify a username (phone number) when linking");
103 } else if (!ns
.getBoolean("dbus") && !ns
.getBoolean("dbus_system")) {
104 if (ns
.getString("username") == null) {
106 System
.err
.println("You need to specify a username (phone number)");
109 if (!PhoneNumberFormatter
.isValidNumber(ns
.getString("username"), null)) {
110 System
.err
.println("Invalid username (phone number), make sure you include the country code.");
115 if (ns
.getList("recipient") != null && !ns
.getList("recipient").isEmpty() && ns
.getString("group") != null) {
116 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");
123 private static ArgumentParser
buildArgumentParser() {
124 ArgumentParser parser
= buildBaseArgumentParser();
126 Subparsers subparsers
= parser
.addSubparsers()
127 .title("subcommands")
129 .description("valid subcommands")
130 .help("additional help");
132 final Map
<String
, Command
> commands
= Commands
.getCommands();
133 for (Map
.Entry
<String
, Command
> entry
: commands
.entrySet()) {
134 Subparser subparser
= subparsers
.addParser(entry
.getKey());
135 entry
.getValue().attachToSubparser(subparser
);
141 private static ArgumentParser
buildBaseArgumentParser() {
142 ArgumentParser parser
= ArgumentParsers
.newFor("signal-cli")
145 .description("Commandline interface for Signal.")
146 .version(BaseConfig
.PROJECT_NAME
+ " " + BaseConfig
.PROJECT_VERSION
);
148 parser
.addArgument("-v", "--version").help("Show package version.").action(Arguments
.version());
149 parser
.addArgument("--verbose")
150 .help("Raise log level and include lib signal logs.")
151 .action(Arguments
.storeTrue());
152 parser
.addArgument("--config")
153 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
155 MutuallyExclusiveGroup mut
= parser
.addMutuallyExclusiveGroup();
156 mut
.addArgument("-u", "--username").help("Specify your phone number, that will be used for verification.");
157 mut
.addArgument("--dbus").help("Make request via user dbus.").action(Arguments
.storeTrue());
158 mut
.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments
.storeTrue());
160 parser
.addArgument("-o", "--output")
161 .help("Choose to output in plain text or JSON")
162 .choices("plain-text", "json")
163 .setDefault("plain-text");