introduces picocli to handle command line args
This commit is contained in:
parent
9823b366fe
commit
83889ea4a7
2 changed files with 137 additions and 111 deletions
25
pom.xml
25
pom.xml
|
@ -39,6 +39,11 @@
|
||||||
<artifactId>smack-experimental</artifactId>
|
<artifactId>smack-experimental</artifactId>
|
||||||
<version>4.4.8</version>
|
<version>4.4.8</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>info.picocli</groupId>
|
||||||
|
<artifactId>picocli</artifactId>
|
||||||
|
<version>4.7.6</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -72,7 +77,25 @@
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -8,79 +8,122 @@ import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
|
||||||
import org.jivesoftware.smack.packet.Message;
|
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 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 {
|
public static void main(String[] args) {
|
||||||
if (2 > args.length) {
|
int exitCode = new CommandLine(new MamLoader()).execute(args);
|
||||||
System.err.println("Your JID and the JID of your chatpartner is needed.");
|
System.exit(exitCode);
|
||||||
System.exit(1);
|
}
|
||||||
|
|
||||||
|
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;
|
private void printMessage(MamMessage mamMessage, String withJid) {
|
||||||
boolean debug = false;
|
StringBuilder sb = new StringBuilder();
|
||||||
Date start = null;
|
Message message = mamMessage.getMessage();
|
||||||
Date end = null;
|
|
||||||
int limit = -1;
|
if (withBodyOnly) {
|
||||||
|
if (null != message.getBody()) {
|
||||||
for (int i = 2; i < args.length; i++) {
|
if (withJid.equals(message.getFrom().asBareJid().toString())) {
|
||||||
switch (args[i]) {
|
sb.append("<");
|
||||||
case "-debug":
|
} else {
|
||||||
debug = true;
|
sb.append(">");
|
||||||
break;
|
}
|
||||||
case "-from":
|
sb.append(" ");
|
||||||
start = parseDate(args[++i]);
|
sb.append(SDF.format(mamMessage.getDate()));
|
||||||
break;
|
sb.append(": ");
|
||||||
case "-to":
|
sb.append(message.getBody());
|
||||||
end = parseDate(args[++i]);
|
System.out.println(sb.toString());
|
||||||
break;
|
|
||||||
case "-limit":
|
|
||||||
limit = Integer.parseInt(args[++i]);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
} 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("Loading history for chats with " + jid);
|
||||||
System.out.println("Limitations:");
|
System.out.println("Limitations:");
|
||||||
if (null == start && null == end && -1 == limit) {
|
if (null == from && null == to && -1 == limit) {
|
||||||
System.out.println("None");
|
System.out.println("None");
|
||||||
} else {
|
} else {
|
||||||
if (null != start) {
|
if (null != from) {
|
||||||
System.out.println("Starting from: " + INPUT.format(start));
|
System.out.println("Starting from: " + SDF_DATE.format(from));
|
||||||
}
|
}
|
||||||
if (null != end) {
|
if (null != to) {
|
||||||
System.out.println("Ending at: " + INPUT.format(end));
|
System.out.println("Ending at: " + SDF_DATE.format(to));
|
||||||
}
|
}
|
||||||
if (-1 < limit) {
|
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.");
|
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) {
|
if (debug) {
|
||||||
System.out.println("Debugging is enabled.");
|
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);
|
MamLoadClient mlc = new MamLoadClient(debug);
|
||||||
try {
|
try {
|
||||||
mlc.connectAndLogin(myjid, password);
|
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;
|
List<MamMessage> messages = null;
|
||||||
|
|
||||||
if (start == null && -1 == limit) {
|
if (from == null && -1 == limit) {
|
||||||
messages = mlc.loadHistory(jid);
|
messages = mlc.loadHistory(jid);
|
||||||
//printMessage(messages.get(0), jid, false);
|
} else if (from == null && -1 < limit) {
|
||||||
} else if (start == null && -1 < limit) {
|
|
||||||
messages = mlc.loadHistory(jid, limit);
|
messages = mlc.loadHistory(jid, limit);
|
||||||
} else if (start != null && end == null) {
|
} else if (from != null && to == null) {
|
||||||
messages = mlc.loadHistory(jid, start);
|
messages = mlc.loadHistory(jid, from);
|
||||||
} else {
|
} else {
|
||||||
messages = mlc.loadHistory(jid, start, end);
|
messages = mlc.loadHistory(jid, from, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,51 +163,8 @@ public class MamLoader {
|
||||||
mlc.disconnect();
|
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) {
|
return 0;
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue