implemented bounce messages
[org.ibex.mail.git] / src / org / ibex / mail / ContentType.java
diff --git a/src/org/ibex/mail/ContentType.java b/src/org/ibex/mail/ContentType.java
new file mode 100644 (file)
index 0000000..7fc7ca6
--- /dev/null
@@ -0,0 +1,62 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.mail;
+import static org.ibex.mail.MailException.*;
+import org.ibex.crypto.*;
+import org.ibex.util.*;
+import org.ibex.mail.protocol.*;
+import org.ibex.io.*;
+import org.ibex.js.*;
+import java.util.*;
+import java.net.*;
+import java.io.*;
+
+// multipart/mixed       -- default
+// multipart/parallel    -- order of components does not matter
+// multipart/alternative -- same data, different versions
+// multipart/digest      -- default content-type of components is message/rfc822
+// message/rfc822        -- FIXME
+// message/partial       -- not supported; see RFC 2046, section 5.2.2
+// message/external-body -- not supported; see RFC 2046, section 5.2.3
+// FIXME charsets  US-ASCII, ISO-8559-X, 
+public class ContentType extends org.ibex.js.JSReflection {
+    public final String    type;
+    public final String    subtype;
+    public final String    description;
+    public final String    id;
+    public final String    transferEncoding;
+    public final String    charset;
+    public final boolean   composite;
+    public final boolean   alternative;
+    public final Hash      parameters = new Hash();
+    public ContentType(String header, String description, String id, String transferEncoding) {
+        this.id = id;
+        this.description = description;
+        this.transferEncoding = transferEncoding;
+        if (header == null) { type="text"; subtype="plain"; charset="us-ascii"; alternative=false; composite=false; return; }
+        header = header.trim();
+        if (header.indexOf('/') == -1) {
+            Log.warn(this, "content-type lacks a forward slash: \""+header+"\"");
+            header = "text/plain";
+        }
+        type = header.substring(0, header.indexOf('/')).toLowerCase();
+        header = header.substring(header.indexOf('/') + 1);
+        subtype = (header.indexOf(';') == -1) ? header.toLowerCase() : header.substring(0, header.indexOf(';')).toLowerCase();
+        composite   = type != null && (type.equals("message") || type.equals("multipart"));
+        alternative = composite && subtype.equals("alternative");
+        charset     = parameters.get("charset") == null ? "us-ascii" : parameters.get("charset").toString();
+        if (header.indexOf(';') == -1) return;
+        StringTokenizer st = new StringTokenizer(header.substring(header.indexOf(';') + 1), ";");
+        while(st.hasMoreTokens()) {
+            String key = st.nextToken().trim();
+            if (key.indexOf('=') == -1)
+                throw new Malformed("content-type parameter lacks an equals sign: \""+key+"\"");
+            String val = key.substring(key.indexOf('=')+1).trim();
+            if (val.startsWith("\"") && val.endsWith("\"")) val = val.substring(1, val.length() - 2);
+            key = key.substring(0, key.indexOf('=')+1).toLowerCase();
+            parameters.put(key, val);
+        }
+    }
+}