From 5cacf94ecca5b45ffdaba0d85506d64117643fd8 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sun, 12 Nov 2017 14:22:01 +0100 Subject: Initial commit --- src/de/thedevstack/xmpp/mamloader/MamLoader.java | 135 +++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/de/thedevstack/xmpp/mamloader/MamLoader.java (limited to 'src/de/thedevstack/xmpp/mamloader/MamLoader.java') diff --git a/src/de/thedevstack/xmpp/mamloader/MamLoader.java b/src/de/thedevstack/xmpp/mamloader/MamLoader.java new file mode 100644 index 0000000..f60c91a --- /dev/null +++ b/src/de/thedevstack/xmpp/mamloader/MamLoader.java @@ -0,0 +1,135 @@ +package de.thedevstack.xmpp.mamloader; + +import java.io.BufferedReader; +import java.io.Console; +import java.io.IOException; +import java.io.InputStreamReader; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smackx.forward.packet.Forwarded; + +public class MamLoader { + private static final SimpleDateFormat SDF = new SimpleDateFormat("dd.MM.YYYY HH:mm:ss"); + private static final SimpleDateFormat INPUT = new SimpleDateFormat("dd.MM.YYYY"); + + 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); + } + 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; + } + } + + 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); + } + + 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); + 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 messages = null; + + if (start == null && -1 == limit) { + messages = mlc.loadHistory(jid); + } else if (start == null && -1 < limit) { + messages = mlc.loadHistory(jid, limit); + } else if (start != null && end == null) { + messages = mlc.loadHistory(jid, start); + } else { + messages = mlc.loadHistory(jid, start, end); + } + + + System.out.println(messages.size() + " messages found"); + printMessagesWithBody(messages, jid); + } finally { + 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 messages, String withJid) { + for (Forwarded f : messages) { + Stanza stanza = f.getForwardedStanza(); + if (stanza instanceof Message) { + StringBuilder sb = new StringBuilder(); + Message message = (Message) stanza; + + if (null != message.getBody()) { + if (withJid.equals(message.getFrom().asBareJid().toString())) { + sb.append("<"); + } else { + sb.append(">"); + } + sb.append(" "); + sb.append(SDF.format(f.getDelayInformation().getStamp())); + sb.append(": "); + sb.append(message.getBody()); + System.out.println(sb.toString()); + } + } + } + } + +} -- cgit v1.2.3