introduces picocli to handle command line args

This commit is contained in:
Tristan 2025-01-09 12:20:57 +01:00
parent 9823b366fe
commit 83889ea4a7
2 changed files with 137 additions and 111 deletions

25
pom.xml
View file

@ -39,6 +39,11 @@
<artifactId>smack-experimental</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.7.6</version>
</dependency>
</dependencies>
<build>
@ -72,7 +77,25 @@
</configuration>
</execution>
</executions>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- annotationProcessorPaths requires maven-compiler-plugin version 3.5 or higher -->
<version>${maven-compiler-plugin-version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>4.7.6</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Aproject=${project.groupId}/${project.artifactId}</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -8,79 +8,122 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Body;
public class MamLoader {
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "MamLoader")
public class MamLoader implements Callable<Integer> {
private static final String DATE_INPUT_FORMAT = "yyyy-mm-dd";
private static final SimpleDateFormat SDF = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
private static final SimpleDateFormat INPUT = new SimpleDateFormat("dd.MM.yyyy");
private static final SimpleDateFormat SDF_DATE = new SimpleDateFormat("dd.MM.yyyy");
@Option(names = {"-d", "--debug"}, description = "Enables debugging output")
private boolean debug = false;
@Option(names = {"-f", "--from"}, description = "Limit loading history beginning with date. Date needs to be formatted as: " + DATE_INPUT_FORMAT)
private Date from;
@Option(names = {"-t", "--to"}, description = "Limit loading history ending with date. Date needs to be formatted as: " + DATE_INPUT_FORMAT)
private Date to;
@Option(names = {"-l", "--limit"}, description = "Maximum number of messages")
private int limit = -1;
@Parameters(description = "Your JID", arity = "1")
private String myjid;
@Parameters(description = "The JID of your chatpartner", arity = "1")
private String jid;
@Option(names = {"-p", "--password"}, description = "Your password", arity = "0..1", interactive = true)
private String password;
@Option(names = {"-m", "--markers"}, description = "Print also messages with markers")
private boolean printMarkers = false;
@Option(names = {"-b", "--with-body-only"}, description = "Print only messages with body")
private boolean withBodyOnly = true;
public static void main(String[] args) throws InterruptedException, SmackException, IOException, XMPPException {
if (2 > args.length) {
System.err.println("Your JID and the JID of your chatpartner is needed.");
System.exit(1);
public static void main(String[] args) {
int exitCode = new CommandLine(new MamLoader()).execute(args);
System.exit(exitCode);
}
private void printMessagesWithBody(List<MamMessage> messages, String withJid) {
for (MamMessage mamMessage : messages) {
printMessage(mamMessage, withJid);
}
String myjid = args[0];
String jid = args[1];
String password;
boolean debug = false;
Date start = null;
Date end = null;
int limit = -1;
for (int i = 2; i < args.length; i++) {
switch (args[i]) {
case "-debug":
debug = true;
break;
case "-from":
start = parseDate(args[++i]);
break;
case "-to":
end = parseDate(args[++i]);
break;
case "-limit":
limit = Integer.parseInt(args[++i]);
break;
}
private void printMessage(MamMessage mamMessage, String withJid) {
StringBuilder sb = new StringBuilder();
Message message = mamMessage.getMessage();
if (withBodyOnly) {
if (null != message.getBody()) {
if (withJid.equals(message.getFrom().asBareJid().toString())) {
sb.append("<");
} else {
sb.append(">");
}
sb.append(" ");
sb.append(SDF.format(mamMessage.getDate()));
sb.append(": ");
sb.append(message.getBody());
System.out.println(sb.toString());
}
}
if (null != end && null == start) {
System.err.println("End without start not supported.");
System.exit(1);
} else if (null != start && -1 < limit) {
System.err.println("Limit not supported if start date is given.");
System.exit(1);
} else if (null == jid || null == myjid) {
System.err.println("Your JID and the JID of your chatpartner is needed.");
System.exit(1);
}
System.out.println("Your JID: " + myjid);
Console console = System.console();
if (null == console) {
System.out.println("Testenvironment!");
System.out.println("Warning: Password will be shown.");
System.out.print("Password: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
password = reader.readLine();
} else {
password = String.valueOf(console.readPassword("Password: "));
if (withJid.equals(message.getFrom().asBareJid().toString())) {
sb.append("<");
} else {
sb.append(">");
}
sb.append(" ");
sb.append(SDF.format(mamMessage.getDate()));
sb.append(": ");
if (null != message.getBody()) {
sb.append(message.getBody());
}
if (printMarkers) {
sb.append("(");
sb.append(message.getExtensions().stream()
.filter(item -> !(item instanceof Body))
.map(ExtensionElement::toXML)
.collect(Collectors.joining(", ")));
sb.append(")");
}
System.out.println(sb.toString());
}
}
@Override
public Integer call() throws Exception {
System.out.println("Your own JID: " + myjid);
if (null == this.password) {
Console console = System.console();
if (null == console) {
System.out.println("Testenvironment!");
System.out.println("Warning: Password will be shown.");
System.out.print("Password: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
password = reader.readLine();
} else {
password = String.valueOf(console.readPassword("Password: "));
}
}
System.out.println("Loading history for chats with " + jid);
System.out.println("Limitations:");
if (null == start && null == end && -1 == limit) {
if (null == from && null == to && -1 == limit) {
System.out.println("None");
} else {
if (null != start) {
System.out.println("Starting from: " + INPUT.format(start));
if (null != from) {
System.out.println("Starting from: " + SDF_DATE.format(from));
}
if (null != end) {
System.out.println("Ending at: " + INPUT.format(end));
if (null != to) {
System.out.println("Ending at: " + SDF_DATE.format(to));
}
if (-1 < limit) {
System.out.println("Only collecting " + limit + " messages. The limit is applied to all messages but only messages containing a body are printed out.");
@ -89,22 +132,25 @@ public class MamLoader {
if (debug) {
System.out.println("Debugging is enabled.");
}
if (printMarkers) {
System.out.println("Printing messages markers.");
}
if (withBodyOnly) {
System.out.println("Printing only messages with body.");
}
MamLoadClient mlc = new MamLoadClient(debug);
try {
mlc.connectAndLogin(myjid, password);
// mlc.loadHistory(jid, Date.from(LocalDate.of(2017, 11, 8).atStartOfDay().toInstant(ZoneOffset.UTC)), Date.from(LocalDate.of(2017, 11, 9).atStartOfDay().toInstant(ZoneOffset.UTC)));
// mlc.loadHistory(jid);
List<MamMessage> messages = null;
if (start == null && -1 == limit) {
if (from == null && -1 == limit) {
messages = mlc.loadHistory(jid);
//printMessage(messages.get(0), jid, false);
} else if (start == null && -1 < limit) {
} else if (from == null && -1 < limit) {
messages = mlc.loadHistory(jid, limit);
} else if (start != null && end == null) {
messages = mlc.loadHistory(jid, start);
} else if (from != null && to == null) {
messages = mlc.loadHistory(jid, from);
} else {
messages = mlc.loadHistory(jid, start, end);
messages = mlc.loadHistory(jid, from, to);
}
@ -117,51 +163,8 @@ public class MamLoader {
mlc.disconnect();
}
}
}
private static Date parseDate(String dateToParse) {
try {
return INPUT.parse(dateToParse);
} catch (ParseException e) {
System.err.println("Could not parse date: " + dateToParse);
System.err.println("Needed format: dd.MM.yyyy");
System.exit(1);
return null;
}
}
private static void printMessagesWithBody(List<MamMessage> messages, String withJid) {
for (MamMessage mamMessage : messages) {
printMessage(mamMessage, withJid, true);
}
}
private static void printMessage(MamMessage mamMessage, String withJid, boolean onlyWithBody) {
StringBuilder sb = new StringBuilder();
Message message = mamMessage.getMessage();
if (onlyWithBody && null != message.getBody()) {
if (withJid.equals(message.getFrom().asBareJid().toString())) {
sb.append("<");
} else {
sb.append(">");
}
sb.append(" ");
sb.append(SDF.format(mamMessage.getDate()));
sb.append(": ");
sb.append(message.getBody());
System.out.println(sb.toString());
} else if (!onlyWithBody) {
if (withJid.equals(message.getFrom().asBareJid().toString())) {
sb.append("<");
} else {
sb.append(">");
}
sb.append(" ");
sb.append(SDF.format(mamMessage.getDate()));
System.out.println(sb.toString());
}
return 0;
}
}