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
.ArgumentParserException
;
22 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
24 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
25 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
26 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
27 import org
.asamk
.signal
.commands
.exceptions
.UntrustedKeyErrorException
;
28 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
29 import org
.asamk
.signal
.manager
.Manager
;
30 import org
.asamk
.signal
.util
.SecurityProvider
;
31 import org
.bouncycastle
.jce
.provider
.BouncyCastleProvider
;
32 import org
.slf4j
.bridge
.SLF4JBridgeHandler
;
34 import java
.security
.Security
;
38 public static void main(String
[] args
) {
39 // enable unlimited strength crypto via Policy, supported on relevant JREs
40 Security
.setProperty("crypto.policy", "unlimited");
41 installSecurityProviderWorkaround();
43 // Configuring the logger needs to happen before any logger is initialized
44 final var verboseLevel
= getVerboseLevel(args
);
45 configureLogging(verboseLevel
);
47 var parser
= App
.buildArgumentParser();
49 var ns
= parser
.parseArgsOrFail(args
);
54 } catch (CommandException e
) {
55 System
.err
.println(e
.getMessage());
56 if (verboseLevel
> 0 && e
.getCause() != null) {
57 e
.getCause().printStackTrace();
59 status
= getStatusForError(e
);
60 } catch (Throwable e
) {
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 int getVerboseLevel(String
[] args
) {
74 var parser
= ArgumentParsers
.newFor("signal-cli").build().defaultHelp(false);
75 parser
.addArgument("--verbose").action(Arguments
.count());
79 ns
= parser
.parseKnownArgs(args
, null);
80 } catch (ArgumentParserException e
) {
84 return ns
.getInt("verbose");
87 private static void configureLogging(final int verboseLevel
) {
88 final var defaultLogLevel
= verboseLevel
> 1 ?
"trace" : verboseLevel
> 0 ?
"debug" : "info";
89 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", defaultLogLevel
);
90 if (verboseLevel
> 0) {
91 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
92 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
93 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
94 System
.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
95 java
.util
.logging
.Logger
.getLogger("")
96 .setLevel(verboseLevel
> 2 ? java
.util
.logging
.Level
.FINEST
: java
.util
.logging
.Level
.INFO
);
99 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
100 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
101 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
103 SLF4JBridgeHandler
.removeHandlersForRootLogger();
104 SLF4JBridgeHandler
.install();
107 private static int getStatusForError(final CommandException e
) {
108 if (e
instanceof UserErrorException
) {
110 } else if (e
instanceof UnexpectedErrorException
) {
112 } else if (e
instanceof IOErrorException
) {
114 } else if (e
instanceof UntrustedKeyErrorException
) {