]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/Main.java
45945c0211f6e6f1252e2bf81c0d1eaca6fb0781
[signal-cli] / src / main / java / org / asamk / signal / Main.java
1 /*
2 Copyright (C) 2015-2021 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.ArgumentParser;
22 import net.sourceforge.argparse4j.inf.ArgumentParserException;
23 import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
24 import net.sourceforge.argparse4j.inf.Namespace;
25 import net.sourceforge.argparse4j.inf.Subparser;
26 import net.sourceforge.argparse4j.inf.Subparsers;
27
28 import org.asamk.signal.commands.Command;
29 import org.asamk.signal.commands.Commands;
30 import org.asamk.signal.manager.LibSignalLogger;
31 import org.asamk.signal.util.SecurityProvider;
32 import org.bouncycastle.jce.provider.BouncyCastleProvider;
33
34 import java.security.Security;
35 import java.util.Map;
36
37 public class Main {
38
39 public static void main(String[] args) {
40 installSecurityProviderWorkaround();
41
42 // Configuring the logger needs to happen before any logger is initialized
43 if (isVerbose(args)) {
44 System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug");
45 System.setProperty("org.slf4j.simpleLogger.showThreadName", "true");
46 System.setProperty("org.slf4j.simpleLogger.showShortLogName", "false");
47 System.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
48 System.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd'T'HH:mm:ss.SSSXX");
49 LibSignalLogger.initLogger();
50 } else {
51 System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
52 System.setProperty("org.slf4j.simpleLogger.showThreadName", "false");
53 System.setProperty("org.slf4j.simpleLogger.showShortLogName", "true");
54 System.setProperty("org.slf4j.simpleLogger.showDateTime", "false");
55 }
56
57 Namespace ns = parseArgs(args);
58 if (ns == null) {
59 System.exit(2);
60 }
61
62 int res = new Cli(ns).init();
63 System.exit(res);
64 }
65
66 private static void installSecurityProviderWorkaround() {
67 // Register our own security provider
68 Security.insertProviderAt(new SecurityProvider(), 1);
69 Security.addProvider(new BouncyCastleProvider());
70 }
71
72 private static boolean isVerbose(String[] args) {
73 ArgumentParser parser = buildBaseArgumentParser();
74
75 Namespace ns;
76 try {
77 ns = parser.parseKnownArgs(args, null);
78 } catch (ArgumentParserException e) {
79 return false;
80 }
81
82 return ns.getBoolean("verbose");
83 }
84
85 private static Namespace parseArgs(String[] args) {
86 ArgumentParser parser = buildArgumentParser();
87
88 Namespace ns;
89 try {
90 ns = parser.parseArgs(args);
91 } catch (ArgumentParserException e) {
92 parser.handleError(e);
93 return null;
94 }
95
96 return ns;
97 }
98
99 private static ArgumentParser buildArgumentParser() {
100 ArgumentParser parser = buildBaseArgumentParser();
101
102 Subparsers subparsers = parser.addSubparsers()
103 .title("subcommands")
104 .dest("command")
105 .description("valid subcommands")
106 .help("additional help");
107
108 final Map<String, Command> commands = Commands.getCommands();
109 for (Map.Entry<String, Command> entry : commands.entrySet()) {
110 Subparser subparser = subparsers.addParser(entry.getKey());
111 entry.getValue().attachToSubparser(subparser);
112 }
113
114 return parser;
115 }
116
117 private static ArgumentParser buildBaseArgumentParser() {
118 ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
119 .build()
120 .defaultHelp(true)
121 .description("Commandline interface for Signal.")
122 .version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);
123
124 parser.addArgument("-v", "--version").help("Show package version.").action(Arguments.version());
125 parser.addArgument("--verbose")
126 .help("Raise log level and include lib signal logs.")
127 .action(Arguments.storeTrue());
128 parser.addArgument("--config")
129 .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
130
131 parser.addArgument("-u", "--username").help("Specify your phone number, that will be used for verification.");
132
133 MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
134 mut.addArgument("--dbus").help("Make request via user dbus.").action(Arguments.storeTrue());
135 mut.addArgument("--dbus-system").help("Make request via system dbus.").action(Arguments.storeTrue());
136
137 parser.addArgument("-o", "--output")
138 .help("Choose to output in plain text or JSON")
139 .choices("plain-text", "json")
140 .setDefault("plain-text");
141
142 return parser;
143 }
144 }