2 Copyright (C) 2015-2022 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
.DefaultSettings
;
21 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
22 import net
.sourceforge
.argparse4j
.inf
.ArgumentParserException
;
23 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
25 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
26 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
27 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
28 import org
.asamk
.signal
.commands
.exceptions
.UntrustedKeyErrorException
;
29 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
30 import org
.asamk
.signal
.manager
.ManagerLogger
;
31 import org
.asamk
.signal
.util
.SecurityProvider
;
32 import org
.bouncycastle
.jce
.provider
.BouncyCastleProvider
;
33 import org
.slf4j
.bridge
.SLF4JBridgeHandler
;
36 import java
.security
.Security
;
40 public static void main(String
[] args
) {
41 // enable unlimited strength crypto via Policy, supported on relevant JREs
42 Security
.setProperty("crypto.policy", "unlimited");
43 installSecurityProviderWorkaround();
45 // Configuring the logger needs to happen before any logger is initialized
47 final var nsLog
= parseArgs(args
);
48 final var verboseLevel
= nsLog
== null ?
0 : nsLog
.getInt("verbose");
49 final var logFile
= nsLog
== null ?
null : nsLog
.<File
>get("log-file");
50 configureLogging(verboseLevel
, logFile
);
52 var parser
= App
.buildArgumentParser();
54 var ns
= parser
.parseArgsOrFail(args
);
59 } catch (CommandException e
) {
60 System
.err
.println(e
.getMessage());
61 if (verboseLevel
> 0 && e
.getCause() != null) {
62 e
.getCause().printStackTrace();
64 status
= getStatusForError(e
);
65 } catch (Throwable e
) {
72 private static void installSecurityProviderWorkaround() {
73 // Register our own security provider
74 Security
.insertProviderAt(new SecurityProvider(), 1);
75 Security
.addProvider(new BouncyCastleProvider());
78 private static Namespace
parseArgs(String
[] args
) {
79 var parser
= ArgumentParsers
.newFor("signal-cli", DefaultSettings
.VERSION_0_9_0_DEFAULT_SETTINGS
)
80 .includeArgumentNamesAsKeysInResult(true)
83 parser
.addArgument("--verbose").action(Arguments
.count());
84 parser
.addArgument("--log-file").type(File
.class);
87 return parser
.parseKnownArgs(args
, null);
88 } catch (ArgumentParserException e
) {
93 private static void configureLogging(final int verboseLevel
, final File logFile
) {
94 LogConfigurator
.setVerboseLevel(verboseLevel
);
95 LogConfigurator
.setLogFile(logFile
);
97 if (verboseLevel
> 0) {
98 java
.util
.logging
.Logger
.getLogger("")
99 .setLevel(verboseLevel
> 2 ? java
.util
.logging
.Level
.FINEST
: java
.util
.logging
.Level
.INFO
);
100 ManagerLogger
.initLogger();
102 SLF4JBridgeHandler
.removeHandlersForRootLogger();
103 SLF4JBridgeHandler
.install();
106 private static int getStatusForError(final CommandException e
) {
107 if (e
instanceof UserErrorException
) {
109 } else if (e
instanceof UnexpectedErrorException
) {
111 } else if (e
instanceof IOErrorException
) {
113 } else if (e
instanceof UntrustedKeyErrorException
) {