1 21 22 package org.jsmtpd.core.mail; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutput ; 29 import java.io.ObjectOutputStream ; 30 import java.io.RandomAccessFile ; 31 import java.io.Serializable ; 32 import java.util.Date ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 36 import javax.mail.internet.MimeBodyPart ; 37 import javax.mail.internet.MimeMessage ; 38 import javax.mail.internet.MimeMultipart ; 39 40 import org.jsmtpd.core.common.io.InvalidStreamParserInitialisation; 41 import org.jsmtpd.core.common.io.dataStream.DataStreamParser; 42 import org.jsmtpd.tools.DateUtil; 43 44 52 public class Email implements Serializable { 53 56 private String authContext = null; 57 60 private Date arrival; 61 64 private int attemps=0; 65 66 69 private List <Rcpt> rcpt = new LinkedList <Rcpt>(); 70 73 private EmailAddress from; 74 77 private String receivedFrom; 78 79 82 private String diskName = "default.mls"; 83 86 private boolean filtered = false; 87 90 private byte[] dataBuffer = null; 91 92 public Email() { 93 generateDiskName(); 94 } 95 96 99 public void addRcpt(EmailAddress in) { 100 rcpt.add(new Rcpt(in)); 101 } 102 103 107 public Date getArrival() { 108 return arrival; 109 } 110 111 public void setArrival(Date arrival) { 112 this.arrival = arrival; 113 } 114 115 public int getAttemps() { 116 return attemps; 117 } 118 119 public void setAttemps(int attemps) { 120 this.attemps = attemps; 121 } 122 123 public void increaseAttempts () { 124 attemps++; 125 } 126 127 public String getReceivedFrom() { 128 return receivedFrom; 129 } 130 131 public void setReceivedFrom(String receivedFrom) { 132 this.receivedFrom = receivedFrom; 133 } 134 135 public List <Rcpt> getRcpt() { 136 return rcpt; 137 } 138 139 143 public String getRcptAsString() { 144 String rcp = ""; 145 for (Rcpt elm : rcpt) { 146 rcp += elm.getEmailAddress().toString() + ";"; 147 } 148 return rcp; 149 } 150 151 154 public String toString() { 155 String out = "Message Dump\n"; 156 157 out += "Received on " + arrival + " From " + receivedFrom + "\n"; 158 out += "Attempts :" + attemps + "\n"; 159 out += "FROM :" + from + "\n"; 160 Rcpt re; 161 for (int i = 0; i < rcpt.size(); i++) { 162 re = (Rcpt) rcpt.get(i); 163 out += "RCPT: " + re.getEmailAddress().toString() + "\n"; 164 } 165 166 return out; 167 } 168 169 public EmailAddress getFrom() { 170 return from; 171 } 172 173 public void setFrom(EmailAddress from) { 174 this.from = from; 175 } 176 177 public long getSize() { 178 if (dataBuffer == null) 179 return 0; 180 return dataBuffer.length; 181 } 182 183 189 public static void save(String fileName, Email e) throws IOException { 190 ObjectOutput out = new ObjectOutputStream (new FileOutputStream (fileName)); 191 out.writeObject(e); 192 out.close(); 193 } 194 195 201 public static Email load(String fileName) throws IOException { 202 RandomAccessFile fp = new RandomAccessFile (fileName, "r"); 203 byte[] data = new byte[(int) fp.length()]; 204 fp.read(data); 205 fp.close(); 206 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (data)); 207 Email e = null; 208 try { 209 e = (Email) in.readObject(); 210 } catch (ClassNotFoundException cnfe) { 211 cnfe.printStackTrace(); 212 } 213 in.close(); 214 return e; 215 } 216 217 221 public boolean isDelivered() { 222 for (Rcpt elm : rcpt) { 223 if (!elm.isDelivered()) 224 return false; 225 } 226 return true; } 227 228 232 public byte[] getDataAsByte() { 233 return dataBuffer; 234 } 235 236 244 public static Email createInternalMail(EmailAddress to, String subject, LinkedList <String > mailMessages, Email attach) { 245 Email e = new Email(); 246 e.generateDiskName(); 247 EmailAddress from = new EmailAddress(); 248 e.setFiltered(true); 249 from.setUser("<>"); 250 e.setFrom(from); 251 e.addRcpt(to); 252 e.setArrival(new Date ()); 253 e.setReceivedFrom("jsmtpd mailer daemon"); 254 255 try { 256 DataStreamParser dsp = new DataStreamParser(512, 1024 * 1024 * 10); 257 dsp.appendString("Message-ID: <" + e.getDiskName() + ">"); 258 dsp.appendString("From: \"Jsmtpd Mailer-Daemon\" <>"); 259 dsp.appendString("To: <" + to.toString() + ">"); 260 dsp.appendString("Subject: " + subject); 261 dsp.appendString("Date: " + DateUtil.currentRFCDate()); 262 dsp.appendString("Content-Type: text/plain; charset=\"ISO-8859-1\""); 263 dsp.appendString("Content-Transfer-Encoding: 7bit"); 264 dsp.appendString(""); 265 for (String string : mailMessages) { 266 dsp.appendString(string); 267 } 268 e.setDataBuffer(dsp.getData()); 269 } catch (InvalidStreamParserInitialisation e1) { 270 } 271 272 return e; 273 } 274 275 public String generateDiskName() { 276 return diskName = Thread.currentThread().getId() + "-" + System.currentTimeMillis() + ".eml"; 277 } 278 279 public String getDiskName() { 280 return diskName; 281 } 282 283 public void setDiskName(String diskName) { 284 this.diskName = diskName; 285 } 286 287 public boolean isFiltered() { 288 return filtered; 289 } 290 291 public void setFiltered(boolean filtered) { 292 this.filtered = filtered; 293 } 294 295 public void setDataBuffer(byte[] dataBuffer) { 296 this.dataBuffer = dataBuffer; 297 } 298 299 public String getAuthContext() { 300 return authContext; 301 } 302 303 public void setAuthContext(String authContext) { 304 this.authContext = authContext; 305 } 306 307 public boolean hasRecipient (String in) { 308 for (Rcpt rc : rcpt) { 309 if (rc.getEmailAddress().toString().equals(in)) 310 return true; 311 } 312 return false; 313 } 314 315 public boolean contains (String in) throws Exception { 316 if (dataBuffer==null) 317 return false; 318 MimeMessage msg = new MimeMessage (null,new ByteArrayInputStream (getDataAsByte())); 319 if (msg.isMimeType("text/plain")) 320 return msg.getContent().toString().contains(in) || msg.getContent().equals(""); 321 if (msg.isMimeType("text/html")) 322 return msg.getContent().toString().contains(in)|| msg.getContent().equals(""); 323 if (msg.isMimeType("multipart/alternative")) { 324 MimeMultipart mpart = (MimeMultipart ) msg.getContent(); 325 String all = ""; 326 for (int i= 0; i < mpart.getCount(); i++) { 327 MimeBodyPart bodyPart = (MimeBodyPart ) mpart.getBodyPart(i); 328 if (bodyPart.isMimeType("text/html") || bodyPart.isMimeType("text/plain")) 329 all+=bodyPart.getContent().toString(); 330 } 331 return all.contains(in)|| all.equals(""); 332 } 333 return false; 334 } 335 336 public Email(Email original) { 337 setFrom(new EmailAddress(original.getFrom())); 338 byte[] newData = new byte[original.getDataAsByte().length]; 339 System.arraycopy(original.dataBuffer,0,newData,0,newData.length); 340 setDataBuffer(newData); 341 generateDiskName(); 342 if (original.authContext!=null) 343 setAuthContext(new String (original.authContext)); 344 setFiltered(false); 345 if (original.getReceivedFrom()!=null) 346 setReceivedFrom(new String (original.receivedFrom)); 347 List <Rcpt> rcpts = original.getRcpt(); 348 for (Rcpt rcpt : rcpts) { 349 addRcpt(rcpt.getEmailAddress()); 350 } 351 } 352 public Email copy () { 353 return new Email(this); 354 } 355 } | Popular Tags |