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.
6 import static org.ibex.mail.MailException.*;
7 import org.ibex.crypto.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
12 import org.ibex.io.Fountain;
17 // FEATURE: MIME RFC2045, 2046, 2049
19 /** This class contains logic for encoding and decoding MIME multipart messages */
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;
28 private final Fountain all;
29 private final Fountain body;
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; }
36 public Part(Fountain all) {
37 this.headers = new Headers(all);
38 String ctype = headers.get("content-type");
39 this.encoding = headers.get("content-transfer-encoding");
40 String enc = this.encoding;
41 if (enc!=null) enc = enc.toLowerCase();
42 if (!(enc == null || enc.equals("7bit") || enc.equals("8bit") || enc.equals("binary") ||
43 enc.equals("quoted-printable") || enc.equals("base64"))) {
44 Log.warn(MIME.class, "unknown TransferEncoding \"" + encoding + "\"");
45 ctype = "application/octet-stream";
47 this.contentType = new ContentType(ctype, headers.get("content-description"), headers.get("content-id"), encoding);
49 this.body = new Fountain() {
50 public int getNumLines() { return Stream.countLines(getStream()); }
51 public long getLength() { return Part.this.getLength() - headers.getLength() - 2 /*CRLF*/; }
52 public Stream getStream() { return transformBodyStream(Headers.skip(Part.this.all.getStream())); }
56 private Stream transformBodyStream(Stream body) {
57 //"quoted-printable".equals(encoding) ? Encode.QuotedPrintable.decode(body.toString(),false) :
58 //"base64".equals(encoding) ? Encode.fromBase64(body.toString()) :
63 public Part getPart(int i) {
64 Stream stream = body.getStream();
66 // first part begins with a boundary delimiter
67 for(String s = stream.readln(); s != null; s = stream.readln())
68 if (s.equals("--" + contentType.parameters.get("boundary"))) break;
70 Stream substream = new BoundaryStream(stream, (String)contentType.parameters.get("boundary"));
71 Part p = new Part(substream, true, null); // FIXME split off headers
73 if (substream.isLast()) break;
75 return parts = (Part[])v.copyInto(new Part[v.size()]);
81 public static class Boundary implements Stream.Transformer {
82 private final String boundary;
83 private boolean done = false;
84 private boolean last = false;
85 public Boundary(String bounardy) { this.boundary = boundary; }
86 public boolean isLast() { while(!done) readln(); return last; }
87 public Stream transform(Stream stream) {
88 for(String s = stream.readln(); s != null; s = stream.readln()) {
89 if (boundary != null && (s.equals(boundary) || s.equals(boundary + "--"))) {
90 body.setLength(body.length() - 2); // preceeding CRLF is part of delimiter
91 last = s.equals(boundary + "--");