use of i2p.susi.util.EOFOnMatchInputStream in project i2p.i2p by i2p.
the class Mail method parseHeaders.
/**
* @return all headers, to pass to MailPart, or null on error
*/
private String[] parseHeaders(InputStream in) {
String[] headerLines = null;
error = "";
if (header != null) {
boolean ok = true;
Encoding html = EncodingFactory.getEncoding("HTML");
if (html == null) {
error += "HTML encoder not found.\n";
ok = false;
}
Encoding hl = EncodingFactory.getEncoding("HEADERLINE");
if (hl == null) {
error += "Header line encoder not found.\n";
ok = false;
}
if (ok) {
try {
EOFOnMatchInputStream eofin = new EOFOnMatchInputStream(in, HEADER_MATCH);
MemoryBuffer decoded = new MemoryBuffer(4096);
hl.decode(eofin, decoded);
if (!eofin.wasFound())
Debug.debug(Debug.DEBUG, "EOF hit before \\r\\n\\r\\n in Mail");
// Fixme UTF-8 to bytes to UTF-8
headerLines = DataHelper.split(new String(decoded.getContent(), decoded.getOffset(), decoded.getLength()), "\r\n");
for (int j = 0; j < headerLines.length; j++) {
String line = headerLines[j];
if (line.length() == 0)
break;
String hlc = line.toLowerCase(Locale.US);
if (hlc.startsWith("from:")) {
sender = line.substring(5).trim();
// formattedSender = getAddress( sender );
shortSender = sender.replace("\"", "").trim();
int lt = shortSender.indexOf('<');
if (lt > 0)
shortSender = shortSender.substring(0, lt).trim();
else if (lt < 0 && shortSender.contains("@"))
// add missing <> (but thunderbird doesn't...)
shortSender = '<' + shortSender + '>';
boolean trim = shortSender.length() > 45;
if (trim)
shortSender = ServletUtil.truncate(shortSender, 42).trim();
shortSender = html.encode(shortSender);
if (trim)
// must be after html encode
shortSender += "…";
} else if (hlc.startsWith("date:")) {
dateString = line.substring(5).trim();
long dateLong = RFC822Date.parse822Date(dateString);
if (dateLong > 0)
setDate(dateLong);
} else if (hlc.startsWith("subject:")) {
subject = line.substring(8).trim();
shortSubject = subject;
boolean trim = subject.length() > 75;
if (trim)
shortSubject = ServletUtil.truncate(subject, 72).trim();
shortSubject = html.encode(shortSubject);
if (trim)
// must be after html encode
shortSubject += "…";
} else if (hlc.startsWith("reply-to:")) {
reply = getAddress(line.substring(9).trim());
} else if (hlc.startsWith("to:")) {
ArrayList<String> list = new ArrayList<String>();
getRecipientsFromList(list, line.substring(3).trim(), true);
if (list.isEmpty()) {
// don't set
} else if (to == null) {
to = list.toArray(new String[list.size()]);
} else if (cc == null) {
// Susimail bug before 0.9.33, sent 2nd To line that was really Cc
cc = list.toArray(new String[list.size()]);
} else {
// add to the array, shouldn't happen
for (int i = 0; i < to.length; i++) {
list.add(i, to[i]);
}
to = list.toArray(new String[list.size()]);
}
} else if (hlc.startsWith("cc:")) {
ArrayList<String> list = new ArrayList<String>();
getRecipientsFromList(list, line.substring(3).trim(), true);
if (list.isEmpty()) {
// don't set
} else if (cc == null) {
cc = list.toArray(new String[list.size()]);
} else {
// add to the array, shouldn't happen
for (int i = 0; i < cc.length; i++) {
list.add(i, cc[i]);
}
cc = list.toArray(new String[list.size()]);
}
} else if (hlc.equals("x-spam-flag: yes")) {
// TODO trust.spam.headers config
isSpam = true;
} else if (hlc.startsWith("content-type:")) {
// this is duplicated in MailPart but
// we want to know if we have attachments, even if
// we haven't fetched the body
contentType = line.substring(13).trim();
} else if (hlc.startsWith("message-id:")) {
messageID = line.substring(11).trim();
}
}
} catch (Exception e) {
error += "Error parsing mail header: " + e.getClass().getName() + '\n';
Debug.debug(Debug.ERROR, "Parse error", e);
}
}
}
return headerLines;
}
Aggregations