| 1 |
/* |
| 2 |
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.io.*; |
| 12 |
import jakarta.mail.*; |
| 13 |
import jakarta.mail.internet.*; |
| 14 |
import jakarta.activation.*; |
| 15 |
|
| 16 |
/** |
| 17 |
* This class models a UUEncoded Message sent from MS Outlook etc. <p> |
| 18 |
* |
| 19 |
* The message structure looks like this := |
| 20 |
* [text body] [uuencoded attachment]* |
| 21 |
* <p> |
| 22 |
* i.e., an optional text/plain main-body, followed by zero or more |
| 23 |
* UUENCODE-ed attachments. |
| 24 |
* |
| 25 |
* @author John Mani |
| 26 |
* @author Bill Shannon |
| 27 |
* @see jakarta.mail.internet.MimeMessage |
| 28 |
*/ |
| 29 |
|
| 30 |
public class MSMessage extends MimeMessage { |
| 31 |
private String type; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor that converts a MimeMessage object into a MSMessage. |
| 35 |
* |
| 36 |
* @exception MessagingException if the given MimeMessage |
| 37 |
* is not a non-MIME MS message, or if an |
| 38 |
* IOException occurs when accessing the given |
| 39 |
* MimeMessage object |
| 40 |
*/ |
| 41 |
public MSMessage(Session session, MimeMessage msg) |
| 42 |
throws MessagingException { |
| 43 |
super(session); |
| 44 |
|
| 45 |
if (!isMSMessage(msg)) // sanity check |
| 46 |
throw new MessagingException("Not an MS message"); |
| 47 |
|
| 48 |
class FastByteArrayOutputStream extends ByteArrayOutputStream { |
| 49 |
ByteArrayInputStream toByteArrayInputStream() { |
| 50 |
return new ByteArrayInputStream(buf, 0, count); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// extract the bytes of the given message |
| 55 |
// ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 56 |
FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); |
| 57 |
try { |
| 58 |
msg.writeTo(bos); |
| 59 |
} catch (IOException ioex) { |
| 60 |
throw new MessagingException("IOException", ioex); |
| 61 |
} catch (Exception ex) { |
| 62 |
throw new MessagingException("Exception", ex); |
| 63 |
} |
| 64 |
//parse(new ByteArrayInputStream(bos.toByteArray())); |
| 65 |
parse(bos.toByteArrayInputStream()); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Constructor to create a MSMessage from the given InputStream. |
| 70 |
*/ |
| 71 |
public MSMessage(Session session, InputStream is) |
| 72 |
throws MessagingException { |
| 73 |
super(session); // setup headerstore etc |
| 74 |
parse(is); |
| 75 |
} |
| 76 |
|
| 77 |
// parse input stream |
| 78 |
protected void parse(InputStream is) throws MessagingException { |
| 79 |
// Create a buffered input stream for efficiency |
| 80 |
if (!(is instanceof ByteArrayInputStream) && |
| 81 |
!(is instanceof BufferedInputStream)) |
| 82 |
is = new BufferedInputStream(is); |
| 83 |
|
| 84 |
// Load headerstore |
| 85 |
headers.load(is); |
| 86 |
|
| 87 |
/* |
| 88 |
* Load the content into a byte[]. |
| 89 |
* This byte[] is shared among the bodyparts. |
| 90 |
*/ |
| 91 |
try { |
| 92 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 93 |
int b; |
| 94 |
// XXX - room for performance improvement |
| 95 |
while ((b = is.read()) != -1) |
| 96 |
bos.write(b); |
| 97 |
content = bos.toByteArray(); |
| 98 |
} catch (IOException ioex) { |
| 99 |
throw new MessagingException("IOException", ioex); |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* Check whether this is multipart. |
| 104 |
*/ |
| 105 |
boolean isMulti = false; |
| 106 |
// check for presence of X-MS-Attachment header |
| 107 |
String[] att = getHeader("X-MS-Attachment"); |
| 108 |
if (att != null && att.length > 0) |
| 109 |
isMulti = true; |
| 110 |
else { |
| 111 |
/* |
| 112 |
* Fall back to scanning the content. |
| 113 |
* We scan the content until we find a sequence that looks |
| 114 |
* like the start of a uuencoded block, i.e., "<newline>begin". |
| 115 |
* If found, we claim that this is a multipart message. |
| 116 |
*/ |
| 117 |
for (int i = 0; i < content.length; i++) { |
| 118 |
int b = content[i] & 0xff; // mask higher byte |
| 119 |
if (b == '\r' || b == '\n') { |
| 120 |
// start of a new line |
| 121 |
if ((i + 5) < content.length) { |
| 122 |
// can there be a "begin" now? |
| 123 |
String s = toString(content, i+1, i+6); |
| 124 |
if (s.equalsIgnoreCase("begin")) { |
| 125 |
isMulti= true; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (isMulti) { |
| 134 |
type = "multipart/mixed"; |
| 135 |
dh = new DataHandler(new MSMultipartDataSource(this, content)); |
| 136 |
} else { |
| 137 |
type = "text/plain"; // charset = ? |
| 138 |
dh = new DataHandler(new MimePartDataSource(this)); |
| 139 |
} |
| 140 |
|
| 141 |
modified = false; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Return content-type |
| 146 |
*/ |
| 147 |
public String getContentType() throws MessagingException { |
| 148 |
return type; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Return content-disposition |
| 153 |
*/ |
| 154 |
public String getDisposition() throws MessagingException { |
| 155 |
return "inline"; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Return content-transfer-encoding |
| 160 |
*/ |
| 161 |
public String getEncoding() throws MessagingException { |
| 162 |
return "7bit"; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Check whether the given MimeMessage object represents a |
| 167 |
* non-MIME message sent by Outlook. Such a message will |
| 168 |
* have no MIME-Version header, may have an X-Mailer header |
| 169 |
* that includes the word "Microsoft", and will have at least |
| 170 |
* one X-MS-Attachment header. |
| 171 |
*/ |
| 172 |
public static boolean isMSMessage(MimeMessage msg) |
| 173 |
throws MessagingException { |
| 174 |
// Check whether the MIME header is present |
| 175 |
if (msg.getHeader("MIME-Version") != null) |
| 176 |
// MIME-Version header present, should be a MIME message |
| 177 |
return false; |
| 178 |
|
| 179 |
/* |
| 180 |
* XXX - disabled X-Mailer check because many sample messages |
| 181 |
* I saw didn't have an X-Mailer header at all. |
| 182 |
*/ |
| 183 |
if (false) { |
| 184 |
// Check X-Mailer |
| 185 |
String mailer = msg.getHeader("X-mailer", null); |
| 186 |
if (mailer == null) // No X-mailer ? |
| 187 |
return false; // Oh well ! |
| 188 |
if (mailer.indexOf("Microsoft") == -1) // Not MS stuff ? |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
// Check X-MS-Attachment header |
| 193 |
// XXX - not all such messages have this header |
| 194 |
String[] att = msg.getHeader("X-MS-Attachment"); |
| 195 |
if (att == null || att.length == 0) |
| 196 |
return false; |
| 197 |
|
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
// convert given byte array of ASCII characters to string |
| 202 |
static String toString(byte[] b, int start, int end) { |
| 203 |
int size = end - start; |
| 204 |
char[] theChars = new char[size]; |
| 205 |
|
| 206 |
for (int i = 0, j = start; i < size; ) |
| 207 |
theChars[i++] = (char)b[j++]; |
| 208 |
return new String(theChars); |
| 209 |
} |
| 210 |
} |