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 isVerbose
= isVerbose(args
);
45 configureLogging(isVerbose
);
47 var parser
= App
.buildArgumentParser();
49 var ns
= parser
.parseArgsOrFail(args
);
54 } catch (CommandException e
) {
55 System
.err
.println(e
.getMessage());
56 if (isVerbose
&& 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 boolean isVerbose(String
[] args
) {
74 var parser
= ArgumentParsers
.newFor("signal-cli").build().defaultHelp(false);
75 parser
.addArgument("--verbose").action(Arguments
.storeTrue());
79 ns
= parser
.parseKnownArgs(args
, null);
80 } catch (ArgumentParserException e
) {
84 return Boolean
.TRUE
.equals(ns
.getBoolean("verbose"));
87 private static void configureLogging(final boolean verbose
) {
89 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
90 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
91 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
92 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
93 System
.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
96 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
97 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
98 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
99 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
101 SLF4JBridgeHandler
.removeHandlersForRootLogger();
102 SLF4JBridgeHandler
.install();
103 java
.util
.logging
.Logger
.getLogger("").setLevel(java
.util
.logging
.Level
.FINEST
);
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
) {