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
;
33 import java
.security
.Security
;
37 public static void main(String
[] args
) {
38 // enable unlimited strength crypto via Policy, supported on relevant JREs
39 Security
.setProperty("crypto.policy", "unlimited");
40 installSecurityProviderWorkaround();
42 // Configuring the logger needs to happen before any logger is initialized
43 final var isVerbose
= isVerbose(args
);
44 configureLogging(isVerbose
);
46 var parser
= App
.buildArgumentParser();
48 var ns
= parser
.parseArgsOrFail(args
);
53 } catch (CommandException e
) {
54 System
.err
.println(e
.getMessage());
55 if (isVerbose
&& e
.getCause() != null) {
56 e
.getCause().printStackTrace();
58 status
= getStatusForError(e
);
59 } catch (Throwable e
) {
66 private static void installSecurityProviderWorkaround() {
67 // Register our own security provider
68 Security
.insertProviderAt(new SecurityProvider(), 1);
69 Security
.addProvider(new BouncyCastleProvider());
72 private static boolean isVerbose(String
[] args
) {
73 var parser
= ArgumentParsers
.newFor("signal-cli").build().defaultHelp(false);
74 parser
.addArgument("--verbose").action(Arguments
.storeTrue());
78 ns
= parser
.parseKnownArgs(args
, null);
79 } catch (ArgumentParserException e
) {
83 return Boolean
.TRUE
.equals(ns
.getBoolean("verbose"));
86 private static void configureLogging(final boolean verbose
) {
88 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
89 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
90 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
91 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
92 System
.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
95 System
.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
96 System
.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
97 System
.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
98 System
.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
102 private static int getStatusForError(final CommandException e
) {
103 if (e
instanceof UserErrorException
) {
105 } else if (e
instanceof UnexpectedErrorException
) {
107 } else if (e
instanceof IOErrorException
) {
109 } else if (e
instanceof UntrustedKeyErrorException
) {