aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/thedevstack/xmpp/mamloader/MamLoader.java
blob: da2b42791eb50555c1926427534928ef21263b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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<Forwarded> 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<Forwarded> 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());
				}
			}
		}
	}

}