-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailMessage.java
More file actions
99 lines (80 loc) · 2.92 KB
/
Copy pathEmailMessage.java
File metadata and controls
99 lines (80 loc) · 2.92 KB
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
import java.util.*;
import java.util.regex.*;
public class EmailMessage {
private int messageNumber;
private int sizeOctets;
private String from = "";
private String replyTo = "";
private String subject = "";
private String messageId = "";
private Map<String, String> headers = new LinkedHashMap<>();
private EmailMessage() {
}
public static EmailMessage parse(String raw, int msgNumber, int sizeOctets) {
EmailMessage msg = new EmailMessage();
msg.messageNumber = msgNumber;
msg.sizeOctets = sizeOctets;
String headerSection = raw.split("\r?\n\r?\n", 2)[0];
String unfolded = headerSection.replaceAll("\r?\n[ \t]+", " ");
for (String line : unfolded.split("\r?\n")) {
int colon = line.indexOf(':');
if (colon < 0)
continue;
String name = line.substring(0, colon).trim().toLowerCase();
String value = line.substring(colon + 1).trim();
msg.headers.putIfAbsent(name, value);
}
msg.from = msg.headers.getOrDefault("from", "");
msg.replyTo = msg.headers.getOrDefault("reply-to", "");
msg.subject = msg.headers.getOrDefault("subject", "");
msg.messageId = msg.headers.getOrDefault("message-id", "");
return msg;
}
public String getEffectiveReplyAddress() {
return !replyTo.isEmpty() ? replyTo : from;
}
public static String extractAddress(String headerValue) {
if (headerValue == null || headerValue.isEmpty())
return "";
Matcher m = Pattern.compile("<([^>]+)>").matcher(headerValue);
if (m.find())
return m.group(1).trim();
return headerValue.trim().split("\\s+")[0].trim();
}
public boolean isFromMailingList() {
// Check 1: any List-* header (RFC 2369)
for (String key : headers.keySet())
if (key.startsWith("list-"))
return true;
String prec = headers.getOrDefault("precedence", "").toLowerCase();
if (prec.contains("bulk") || prec.contains("list") || prec.contains("junk"))
return true;
String autoSub = headers.getOrDefault("auto-submitted", "").toLowerCase();
if (!autoSub.isEmpty() && !autoSub.equals("no"))
return true;
if (headers.containsKey("x-auto-reply-to"))
return true;
return false;
}
public boolean hasPrac7Subject() {
return subject.trim().equalsIgnoreCase("prac7");
}
public int getMessageNumber() {
return messageNumber;
}
public int getSizeOctets() {
return sizeOctets;
}
public String getFrom() {
return from;
}
public String getSubject() {
return subject;
}
public String getMessageId() {
return messageId;
}
public String getHeader(String name) {
return headers.getOrDefault(name.toLowerCase(), "");
}
}