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); } 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: ")); } System.out.println("Loading history for chats with " + jid); System.out.println("Limitations:"); if (null == start && null == end && -1 == limit) { System.out.println("None"); } else { if (null != start) { System.out.println("Starting from: " + INPUT.format(start)); } if (null != end) { System.out.println("Ending at: " + INPUT.format(end)); } 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."); } } if (debug) { System.out.println("Debugging is enabled."); } 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()); } } } } }