massive refactoring of Headers class
[org.ibex.mail.git] / src / org / ibex / mail / MIME.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import static org.ibex.mail.MailException.*;
7 import org.ibex.crypto.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
10 import org.ibex.js.*;
11 import org.ibex.io.*;
12 import org.ibex.io.Fountain;
13 import java.util.*;
14 import java.net.*;
15 import java.io.*;
16
17 // FEATURE: MIME RFC2045, 2046, 2049
18
19 /** This class contains logic for encoding and decoding MIME multipart messages */
20 public class MIME {
21
22     /** Part = Headers+Body */
23     public static class Part extends JSReflection implements Fountain {
24         public  final Headers      headers;
25         public  final ContentType  contentType;
26         private final String       encoding;
27
28         private final Fountain     all;
29         private final Fountain     body;
30
31         public Stream getStream()   { return all.getStream(); }
32         public int getNumLines()    { return all.getNumLines(); }
33         public long getLength()     { return all.getLength(); }
34         public Fountain getBody()   { return body; }
35
36         private class BodyFountain implements Fountain {
37             public int getNumLines()  { return Stream.countLines(getStream()); }
38             public long getLength()   { return Part.this.getLength() - headers.getLength() - 2 /*FIXME: correct?*/; }
39             public Stream getStream() {
40                 return /*
41                          "quoted-printable".equals(encoding) ? Encode.QuotedPrintable.decode(body.toString(),false) :
42                          "base64".equals(encoding)           ? Encode.fromBase64(body.toString()) :
43                        */
44                     Headers.Original.skip(all.getStream());
45             }
46         }
47
48         public Part(Fountain all) {
49             this.headers     = new Headers(all);
50             String ctype     = headers.get("content-type");
51             this.encoding    = headers.get("content-transfer-encoding");
52             String enc = this.encoding;
53             if (enc!=null) enc = enc.toLowerCase();
54             if (!(enc == null || enc.equals("7bit") || enc.equals("8bit") || enc.equals("binary") ||
55                   enc.equals("quoted-printable") || enc.equals("base64"))) {
56                 Log.warn(MIME.class, "unknown TransferEncoding \"" + encoding + "\"");
57                 ctype = "application/octet-stream";
58             }
59             this.contentType = new ContentType(ctype, headers.get("content-description"), headers.get("content-id"), encoding);
60             this.all = all;
61             this.body = new BodyFountain();
62         }
63
64         /*
65         public Part getPart(int i) {
66             Stream stream = body.getStream();
67             Vec v = new Vec();
68             // first part begins with a boundary delimiter
69             for(String s = stream.readln(); s != null; s = stream.readln())
70                 if (s.equals("--" + contentType.parameters.get("boundary"))) break;
71             while(true) {
72                 Stream substream = new BoundaryStream(stream, (String)contentType.parameters.get("boundary"));
73                 Part p = new Part(substream, true, null); // FIXME split off headers
74                 v.addElement(p);
75                 if (substream.isLast()) break;
76             }
77             return parts = (Part[])v.copyInto(new Part[v.size()]);
78         }
79         */
80     }
81     /*
82     public static class Boundary implements Stream.Transformer {
83         private final String boundary;
84         private boolean done = false;
85         private boolean last = false;
86         public Boundary(String bounardy) { this.boundary = boundary; }
87         public boolean isLast() { while(!done) readln(); return last; }
88         public Stream transform(Stream stream) {
89             for(String s = stream.readln(); s != null; s = stream.readln()) {
90                 if (boundary != null && (s.equals(boundary) || s.equals(boundary + "--"))) {
91                     body.setLength(body.length() - 2);  // preceeding CRLF is part of delimiter
92                     last = s.equals(boundary + "--");
93                     done = true;
94                     break;
95                 }
96                 body.append(s);
97                 body.append("\r\n");
98                 //lines++;
99             }
100         }
101     }
102     */
103 }