c392d3b41519798064b06d55268077ead2c32d64
[org.ibex.mail.git] / src / org / ibex / mail / Message.java
1 package org.ibex.mail;
2 import org.ibex.crypto.*;
3 import org.ibex.util.*;
4 import org.ibex.mail.protocol.*;
5 import org.ibex.io.*;
6 import java.util.*;
7 import java.net.*;
8 import java.io.*;
9
10 // FIXME this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
11
12 // soft line limit (suggested): 78 chars /  hard line limit: 998 chars
13 // date/time parsing: see spec, 3.3
14
15 // FIXME: messages must NEVER contain 8-bit binary data; this is a violation of IMAP
16
17 // FEATURE: PGP-signature-parsing
18 // FEATURE: mailing list header parsing
19 // FEATURE: delivery status notification (and the sneaky variety)
20 // FEATURE: threading as in http://www.jwz.org/doc/threading.html
21 // FEATURE: lazy body
22 // FIXME RFC822 1,000-char limit per line
23
24 /** 
25  *  [immutable] This class encapsulates a message "floating in the
26  *  ether": RFC2822 data but no storage-specific flags or other
27  *  metadata.
28  */
29 public class Message extends MIME.Part {
30
31     // Parsed Headers //////////////////////////////////////////////////////////////////////////////
32
33     public final Address     to;
34     public final Address     from;                // if multiple From entries, this is sender
35     public final Address     envelopeFrom;
36     public final Address     envelopeTo;
37     public final Date        date;
38     public final Date        arrival;
39     public final Address     replyto;             // if none provided, this is equal to sender
40     public final String      subject;
41     public final String      messageid;
42     public final Address[]   cc;
43     public final Address[]   bcc;
44
45     public static Message newMessage(Stream stream) throws Malformed { return newMessage(stream, null, null); }
46     public static Message newMessage(Stream stream, Address from, Address to) throws Malformed {
47         if (from == null && to == null) return new Message(stream);
48         StringBuffer sb = new StringBuffer();
49         boolean inheaders = true;
50         if (from != null) sb.append("Return-Path: " + from.toString(true) + "\r\n");
51         while(true) {
52             String s = stream.readln();
53             if (s == null) break;
54             if (inheaders && to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
55             if (inheaders && from != null && s.toLowerCase().startsWith("return-path:")) continue;
56             if (s.length() == 0 && inheaders) {
57                 inheaders = false;
58                 if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
59             }
60             sb.append(s);
61             sb.append("\r\n");
62         }
63         return newMessage(new Stream(sb.toString()));
64     }
65     private Message(Stream stream) throws Malformed {
66         super(stream, null, false);
67         this.to           = headers.gets("To") != null ? Address.parse(headers.gets("To")) : 
68                             headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) : null;
69         this.from         = headers.gets("From") != null ? Address.parse(headers.gets("From")) : 
70                             headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) : null;
71         this.envelopeTo   = headers.gets("Envelope-To") != null ? Address.parse(headers.gets("Envelope-To")) :
72                             headers.gets("To") != null ? Address.parse(headers.gets("To")) : null;
73         this.envelopeFrom = headers.gets("Return-Path") != null ? Address.parse(headers.gets("Return-Path")) :
74                             headers.gets("From") != null ? Address.parse(headers.gets("From")) : null;
75         this.replyto      = headers.gets("Reply-To") == null ? null : Address.parse(headers.gets("Reply-To"));
76         this.subject      = headers.gets("Subject");
77         this.messageid    = headers.gets("Message-Id");
78         this.cc           = Address.list(headers.gets("Cc"));
79         this.bcc          = Address.list(headers.gets("BCc"));
80         this.date         = headers.gets("Date") == null ? new Date() : parseDate(headers.gets("Date"));
81         this.arrival      = this.date; // FIXME wrong
82     }
83
84
85     // Helpers /////////////////////////////////////////////////////////////////////////////
86
87     // http://www.jwz.org/doc/mid.html
88     private static final Random random = new Random();
89     public static String generateFreshMessageId() {
90         StringBuffer ret = new StringBuffer();
91         ret.append('<');
92         ret.append(Base36.encode(System.currentTimeMillis()));
93         ret.append('.');
94         ret.append(Base36.encode(random.nextLong()));
95         ret.append('.');
96         try { ret.append(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { /* DELIBERATE */ }
97         ret.append('>');
98         return ret.toString();
99     }
100
101     public static Date parseDate(String s) { return null; } // FIXME!!!
102    
103     //  use null-sender for error messages (don't send errors to the null addr)
104     public Message bounce(String reason) {
105         Log.warn(Message.class, "bounce not implemented");
106         return null;
107     }  // FIXME!
108
109     public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
110
111     public void dump(Stream s) {
112         s.setNewline("\r\n");
113         s.println(headers.raw);
114         s.println("");
115         s.println(body);
116         s.flush();
117     }
118
119     public int size()        { return headers.raw.length() + 2 /* CRLF */ + body.length(); }
120     public String toString() { return headers.raw + "\r\n" + body; }
121
122     public static class Malformed extends Exception { public Malformed(String s) { super(s); } }
123 }
124