]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
Update year
[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.impl.Arguments;
21 import net.sourceforge.argparse4j.inf.ArgumentParserException;
22 import net.sourceforge.argparse4j.inf.Namespace;
23
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;
33
34 import java.security.Security;
35
36 public class Main {
37
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();
42
43 // Configuring the logger needs to happen before any logger is initialized
44 final var verboseLevel = getVerboseLevel(args);
45 configureLogging(verboseLevel);
46
47 var parser = App.buildArgumentParser();
48
49 var ns = parser.parseArgsOrFail(args);
50
51 int status = 0;
52 try {
53 new App(ns).init();
54 } catch (CommandException e) {
55 System.err.println(e.getMessage());
56 if (verboseLevel > 0 && e.getCause() != null) {
57 e.getCause().printStackTrace();
58 }
59 status = getStatusForError(e);
60 } catch (Throwable e) {
61 e.printStackTrace();
62 status = 2;
63 }
64 System.exit(status);
65 }
66
67 private static void installSecurityProviderWorkaround() {
68 // Register our own security provider
69 Security.insertProviderAt(new SecurityProvider(), 1);
70 Security.addProvider(new BouncyCastleProvider());
71 }
72
73 private static int getVerboseLevel(String[] args) {
74 var parser = ArgumentParsers.newFor("signal-cli").build().defaultHelp(false);
75 parser.addArgument("--verbose").action(Arguments.count());
76
77 Namespace ns;
78 try {
79 ns = parser.parseKnownArgs(args, null);
80 } catch (ArgumentParserException e) {
81 return 0;
82 }
83
84 return ns.getInt("verbose");
85 }
86
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);
97 Manager.initLogger();
98 } else {
99 System.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
100 System.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
101 System.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
102 }
103 SLF4JBridgeHandler.removeHandlersForRootLogger();
104 SLF4JBridgeHandler.install();
105 }
106
107 private static int getStatusForError(final CommandException e) {
108 if (e instanceof UserErrorException) {
109 return 1;
110 } else if (e instanceof UnexpectedErrorException) {
111 return 2;
112 } else if (e instanceof IOErrorException) {
113 return 3;
114 } else if (e instanceof UntrustedKeyErrorException) {
115 return 4;
116 } else {
117 return 2;
118 }
119 }
120 }