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 public static class Part extends JSReflection implements Fountain {
23 public final Headers headers;
24 public final ContentType contentType;
25 private final String encoding;
27 private final Fountain all;
28 private final Fountain body;
30 public Stream getStream() { return all.getStream(); }
31 public int getNumLines() { return all.getNumLines(); }
32 public int getLength() { return all.getLength(); }
33 public Fountain getBody() { return body; }
35 private class BodyFountain implements Fountain {
36 public int getNumLines() { return Stream.countLines(getStream()); }
37 public int getLength() { return Part.this.getLength() - headers.getLength() - 2; }
38 public Stream getStream() {
40 "quoted-printable".equals(encoding) ? Encode.QuotedPrintable.decode(body.toString(),false) :
41 "base64".equals(encoding) ? Encode.fromBase64(body.toString()) :
43 Headers.skip(all.getStream());
47 public Part(Fountain all) {
48 this.headers = new Headers(all.getStream());
49 String ctype = headers.get("content-type");
50 this.encoding = headers.get("content-transfer-encoding");
51 if (!(encoding == null || encoding.equals("7bit") || encoding.equals("8bit") || encoding.equals("binary") ||
52 encoding.equals("quoted-printable") || encoding.equals("base64"))) {
53 Log.warn(MIME.class, "unknown TransferEncoding \"" + encoding + "\"");
54 ctype = "application/octet-stream";
56 this.contentType = new ContentType(ctype, headers.get("content-description"), headers.get("content-id"), encoding);
58 this.body = new BodyFountain();
62 public Part getPart(int i) {
63 Stream stream = body.getStream();
65 // first part begins with a boundary delimiter
66 for(String s = stream.readln(); s != null; s = stream.readln())
67 if (s.equals("--" + contentType.parameters.get("boundary"))) break;
69 Stream substream = new BoundaryStream(stream, (String)contentType.parameters.get("boundary"));
70 Part p = new Part(substream, true, null); // FIXME split off headers
72 if (substream.isLast()) break;
74 return parts = (Part[])v.copyInto(new Part[v.size()]);
79 public static class Boundary implements Stream.Transformer {
80 private final String boundary;
81 private boolean done = false;
82 private boolean last = false;
83 public Boundary(String bounardy) { this.boundary = boundary; }
84 public boolean isLast() { while(!done) readln(); return last; }
85 public Stream transform(Stream stream) {
86 for(String s = stream.readln(); s != null; s = stream.readln()) {
87 if (boundary != null && (s.equals(boundary) || s.equals(boundary + "--"))) {
88 body.setLength(body.length() - 2); // preceeding CRLF is part of delimiter
89 last = s.equals(boundary + "--");