]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
Reformat code
[signal-cli] / src / main / java / org / asamk / signal / Main.java
1 /*
2 Copyright (C) 2015-2022 AsamK and contributors
3
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.
8
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.
13
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/>.
16 */
17 package org.asamk.signal;
18
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;
24
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.logging.LogConfigurator;
31 import org.asamk.signal.manager.ManagerLogger;
32 import org.asamk.signal.util.SecurityProvider;
33 import org.bouncycastle.jce.provider.BouncyCastleProvider;
34 import org.slf4j.bridge.SLF4JBridgeHandler;
35
36 import java.io.File;
37 import java.security.Security;
38
39 public class Main {
40
41 public static void main(String[] args) {
42 // enable unlimited strength crypto via Policy, supported on relevant JREs
43 Security.setProperty("crypto.policy", "unlimited");
44 installSecurityProviderWorkaround();
45
46 // Configuring the logger needs to happen before any logger is initialized
47
48 final var nsLog = parseArgs(args);
49 final var verboseLevel = nsLog == null ? 0 : nsLog.getInt("verbose");
50 final var logFile = nsLog == null ? null : nsLog.<File>get("log-file");
51 final var scrubLog = nsLog != null && nsLog.getBoolean("scrub-log");
52 configureLogging(verboseLevel, logFile, scrubLog);
53
54 var parser = App.buildArgumentParser();
55
56 var ns = parser.parseArgsOrFail(args);
57
58 int status = 0;
59 try {
60 new App(ns).init();
61 } catch (CommandException e) {
62 System.err.println(e.getMessage());
63 if (verboseLevel > 0 && e.getCause() != null) {
64 e.getCause().printStackTrace();
65 }
66 status = getStatusForError(e);
67 } catch (Throwable e) {
68 e.printStackTrace();
69 status = 2;
70 }
71 System.exit(status);
72 }
73
74 private static void installSecurityProviderWorkaround() {
75 // Register our own security provider
76 Security.insertProviderAt(new SecurityProvider(), 1);
77 Security.addProvider(new BouncyCastleProvider());
78 }
79
80 private static Namespace parseArgs(String[] args) {
81 var parser = ArgumentParsers.newFor("signal-cli", DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS)
82 .includeArgumentNamesAsKeysInResult(true)
83 .build()
84 .defaultHelp(false);
85 parser.addArgument("-v", "--verbose").action(Arguments.count());
86 parser.addArgument("--log-file").type(File.class);
87 parser.addArgument("--scrub-log").action(Arguments.storeTrue());
88
89 try {
90 return parser.parseKnownArgs(args, null);
91 } catch (ArgumentParserException e) {
92 return null;
93 }
94 }
95
96 private static void configureLogging(final int verboseLevel, final File logFile, final boolean scrubLog) {
97 LogConfigurator.setVerboseLevel(verboseLevel);
98 LogConfigurator.setLogFile(logFile);
99 LogConfigurator.setScrubSensitiveInformation(scrubLog);
100
101 if (verboseLevel > 0) {
102 java.util.logging.Logger.getLogger("")
103 .setLevel(verboseLevel > 2 ? java.util.logging.Level.FINEST : java.util.logging.Level.INFO);
104 ManagerLogger.initLogger();
105 }
106 SLF4JBridgeHandler.removeHandlersForRootLogger();
107 SLF4JBridgeHandler.install();
108 }
109
110 private static int getStatusForError(final CommandException e) {
111 if (e instanceof UserErrorException) {
112 return 1;
113 } else if (e instanceof UnexpectedErrorException) {
114 return 2;
115 } else if (e instanceof IOErrorException) {
116 return 3;
117 } else if (e instanceof UntrustedKeyErrorException) {
118 return 4;
119 } else {
120 return 2;
121 }
122 }
123 }